Skip to content

Instantly share code, notes, and snippets.

@ever-dev
Last active September 18, 2020 08:24
Show Gist options
  • Save ever-dev/169e7c96ba79fadd7cc6234badc9d4e2 to your computer and use it in GitHub Desktop.
Save ever-dev/169e7c96ba79fadd7cc6234badc9d4e2 to your computer and use it in GitHub Desktop.
The way to make a callback function to Promise.

Use of callback like functions

function someFunction1( callbackFn ) => someFunction1(() => { doSomething(); })
function promiseFunction() {
  return new Promise((resolve) => {
     someFunction1(resolve);
  });
}
=>
promiseFunction().then(() => {
  doSomething();
});

When executing several queries one by one using sequelize is a good example.

sql(query1, () => {
  sql(query2, () => {
    sql(query3);
  })
});
=>
await promiseQuery(query1);
await promiseQuery(query2);
await promiseQuery(query3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment