Skip to content

Commit

Permalink
Add async/await to wrap around the Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
beautifulcoder committed Oct 23, 2022
1 parent d741be7 commit 85fb597
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 38 additions & 0 deletions asyncPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var pipeDelimiter = require('./pipeDelimiter');

async function run() {
await buildFerociousCats({ list: '', delimiter: pipeDelimiter }, 'Panther', getJaguar)
console.log('DONE');
}

function getJaguar(cat) {
return buildFerociousCats(cat, 'Jaguar', getLynx);
}

function getLynx(cat) {
return buildFerociousCats(cat, 'Lynx', getSnowLeopard);
}

function getSnowLeopard(cat) {
return buildFerociousCats(cat, 'Snow Leopard', getLion);
}

function getLion(cat) {
return buildFerociousCats(cat, 'Lion', printList);
}

function printList(cat) {
console.log(cat.list);
}

function buildFerociousCats(cat, returnValue, next) {
return new Promise((resolve) => {
setTimeout(function asyncCall(data) {
var catList = cat.delimiter(cat.list, data);

resolve(next({ list: catList, delimiter: cat.delimiter }));
}, 1, returnValue);
});
}

run().then(() => console.log('DONE DONE'));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"callback-hell": "node callbackHell.js",
"dependency-inversion": "node callbackDependencyInversion.js",
"polymorphic-callback": "node callbackPolymorphic.js",
"promise": "node callbackPromise.js"
"promise": "node callbackPromise.js",
"async": "node asyncPromise.js"
},
"devDependencies": {
"mocha": "3.0.1",
Expand Down

0 comments on commit 85fb597

Please sign in to comment.