i am new to learning javascript. i am currently trying to wrap if condition inside a then function.
below is my code,
const createType = ({
firstTypes = [],
secondTypes = [],
...allType
} = {}, skip = true) => {
const builtType = buildType(allType);
let finalType;
let finalSecondTypes;
return apiPostType(builtType)
.then(waitForSync(cy.apiGetTypeWithRetry))
.then(createdType => {
finalType = keysToCamelCase(createdType);
const firstTypesToCreate = firstTypes.map(
firstType => () =>
cy.createFirstType({
...firstType,
allType: createdType.id,
})
);
the above code works fine. now i want to execute waitForSync(apiGetTypeWithRetry) only if skip is set to false. hence i tried like below,
const createType = ({
firstTypes = [],
secondTypes = [],
...allType
} = {}, skip = true) => {
const builtType = buildType(allType);
let finalType;
let finalSecondTypes;
return apiPostType(builtType)
.then(createdType => {
if(skip = false) {
waitForSync(cy.apiGetTypeWithRetry);
}
})
.then(createdType => { //here createdType is undefined
finalType = keysToCamelCase(createdType);
const firstTypesToCreate = firstTypes.map(
firstType => () =>
cy.createFirstType({
...firstType,
allType: createdType.id,
})
);
but after adding if condition in the .then() function the createdType for the next .then() function is undefined. i am not sure how to pass the promise from the previous .then function.
could someone help me with this. thanks.