Skip to content

Instantly share code, notes, and snippets.

@shijiezhou1
Last active August 16, 2021 15:35
Show Gist options
  • Save shijiezhou1/8ce237d37380a5db3d99728a0fa4aae6 to your computer and use it in GitHub Desktop.
Save shijiezhou1/8ce237d37380a5db3d99728a0fa4aae6 to your computer and use it in GitHub Desktop.
runSequentially function
// the function is to make sure the answer is correctly return
async function runSequentially(functions) {
const resolveLists = []
for (const fu of functions) {
resolveLists.push(await fu());
};
return resolveLists;
}
let counter = 1;
function incrementCounterAsync() {
return new Promise((resolve, reject) => {
counter += 1;
resolve(counter);
});
}
setTimeout(() => {
console.log('start');
});
// setTimeout always last execute
// then and catch only pick one
// finally executing in order
let promise = runSequentially([incrementCounterAsync, incrementCounterAsync]);
if (promise) {
promise
.then(result => {
console.log('good: ', result);
return new Promise((resolve, reject) => {
resolve('in the promise');
});
})
.finally(() => {
console.log('finally');
})
.catch(error => console.log("error" + error))
.finally(() => {
console.log('finally2');
throw new Error("second finally throw error");
})
.then((n) => console.log('2 then', n))
.catch(error2 => console.log('2 error', error2));
setTimeout(() => {
console.log('start2');
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment