Skip to content

Document how to refactor callback-based code using the async npm package to the es6 await/await syntax supported by nodejs v8+

License

Notifications You must be signed in to change notification settings

jmjpro/async-package-to-async-await

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

Refactoring by example

How to refactor callback-based code using the async npm package to use the es6 await/await syntax supported by nodejs v8+

async package es6 async/await
async.auto({
  f1: (callback) => {...},
  f2: 'f1', (res, callback) => {...},
  f3: 'f2', (res, callback) => {...},
}), (err, results) => {
  callback(err, { k1: results.f1, k2: results.f2 });
});
      
const results = {}
results.f1 = await f1(...)
results.f2 = await f2(...)
results.f3 = await f3(...)
return results
      
var isCompleted = 0;
async.whilst(
    function() {
      !isCompleted;
    },
    function(callback) {
        callbackFn((err, res) => {
          if (res.isCompleted) {
            isCompleted = res.isCompleted;
          }
          else {
            setTimeout(callback, 5000);
          }
        })
    },
    function (err) {
        callback(err)
    }
);
      
const isCompleted = false
while( !isCompleted ) {
  const res = await util.promisify(callbackFn)
  if (res.isCompleted) {
    isCompleted = res.isCompleted
  } else {
    await util.promisify(setTimeout).call(5000)
  }
}
      
async.series(
    f1: function f1(cb) {...},
    f2: function f2(cb) {...},
    f3: function f3(cb) {...}
), err => {
  if(err) {
    console.log('error', err);
  } else {
    console.log('info', 'success')
  }
}
);
      
try {
  await f1()
  await f2()
  await f3()
  console.log('info', 'success')
} catch(err) {
  console.log('error', err);
}
      
async.waterfall(
  f1: function f1(cb) {cb(null, 1, 2)},
  f2: function f2(arg1, arg2, cb) {cb(null, 3)},
  f3: function f3(arg1, cb) {cb(null, 'done')}
), (err,result) => {
  if(err) {
    console.log('error', err);
  } else {
    console.log('info', result)
  }
});
      
try {
  const f1Res = await f1()
  const f2Res = await f2(f1Res)
  const f3Res = await f3(f2Res)
  console.log('info', f3Res)
} catch(err) {
  console.log('error', err);
}
      
async.parallel(
  f1: function f1(cb) {...},
  f2: function f2(cb) {...},
  f3: function f3(cb) {...}
), err => {
  if(err) {
    console.log('error', err);
  } else {
    console.log('info', 'success')
  }
});
      
const [res1, res2, res3] = await Promise.all([f1, f2, f3])
      
async.eachSeries(openFiles, saveFile, function(err){
});
      
try {
  for (let f of openFiles) {
    await saveFile(f)
  }
} catch(err) {
  console.log('error', err);
}
      
async.each(openFiles, saveFile, function(err){
});
console.log('done')
      
try {
  const resArr = await Promise.all(
    openFiles.map(async it => await saveFile(it)
  )
  console.log('done')
} catch(err) {
  console.log('error', err);
}
      

About

Document how to refactor callback-based code using the async npm package to the es6 await/await syntax supported by nodejs v8+

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published