i was lookging to replace some callbacks with async and the functions are not fireing in order like they currently do using a callback
Here is example functions using callback that fire off correctly
function doSomething(callback) {
setTimeout(function(){ console.log('FIRE OFF FIRST'); callback();}, 3000);
}
function doSomethingElse(callback) {
setTimeout(function(){ console.log('FIRE OFF SECOND');callback();}, 1000);
}
function doThirdThing(callback) {
setTimeout(function(){ console.log('FIRE OFF LAST');callback();; }, 100);
}
doSomething(function () {
doSomethingElse(function () {
doThirdThing(function () {
console.log('ALL DONE');
});
});
});
Now trying to change over to async and i can't get them to all fire off , only the first promise is working
function doSomething() {
return new Promise(function (resolve, reject) {
setTimeout (function(){
console.log ('FIRE OFF FIRST');
}, 5000);
});
}
function doSomethingElse() {
return new Promise(function (resolve, reject) {
setTimeout (function(){
console.log ('FIRE OFF SECOND');
}, 2000);
});
}
function doThirdThing() {
return new Promise(function (resolve, reject) {
setTimeout (function(){
console.log ('FIRE OFF LAST');
}, 1000);
});
}
(async () => {
try {
const result = await doSomething();
const newResult = await doSomethingElse(result);
const finalResult = await doThirdThing(newResult);
console.log(finalResult);
} catch(err) {
console.log(err);
}
})();
Ok to do the above correctly i needed to resolve each promise
function doSomething() {
return new Promise(function (resolve, reject) {
setTimeout (function(){
console.log ('FIRE OFF FIRST');
resolve();
}, 5000);
});
}
function doSomethingElse() {
return new Promise(function (resolve, reject) {
setTimeout (function(){
console.log ('FIRE OFF SECOND');
resolve();
}, 2000);
});
}
function doThirdThing() {
return new Promise(function (resolve, reject) {
setTimeout (function(){
console.log ('FIRE OFF LAST');
resolve();
}, 1000);
});
}
(async () => {
try {
const result = await doSomething();
const newResult = await doSomethingElse(result);
const finalResult = await doThirdThing(newResult);
console.log(finalResult);
} catch(err) {
console.log(err);
}
})();