Last active
December 13, 2017 13:46
-
-
Save mheap/4abf07d84a7ae08b729d5934d43a4e66 to your computer and use it in GitHub Desktop.
Revisions
-
mheap revised this gist
Dec 13, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ const promiseOrCallback = (promise, callback) => { }; const foo = (a, b, callback) => { const promise = new Promise((resolve, reject) => { return resolve(a + b); }); -
mheap revised this gist
Dec 13, 2017 . 4 changed files with 26 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,12 @@ const promiseOrCallback = (promise, callback) => { if (typeof callback != "function") { return promise; } return promise .then((result) => (callback(null, result))) .catch(callback); }; const foo = (a, b, callback) => { const promise = new Promise((resolve, reject){ return resolve(a + b); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,20 @@ const promiseOrCallback = function(func, callback) { return new Promise(async (resolve, reject) => { try { const data = await func(); if (typeof callback == "function") { return callback(null, data); } return resolve(data); } catch (err) { if (typeof callback == "function") { return callback(err, null); } return reject(err); } }); }; const foo = (a, b, callback) => { return promiseOrCallback(function() { return new Promise((resolve, reject){ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +0,0 @@ -
mheap revised this gist
Dec 13, 2017 . 2 changed files with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ const foo = (a, b, callback) => { const promise = new Promise((resolve, reject){ return resolve(a + b); }); return promiseOrCallback(promise, callback); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ const foo = (a, b, callback) => { return promiseOrCallback(function() { return new Promise((resolve, reject){ return resolve(a + b); }); }, callback); } -
mheap created this gist
Dec 13, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ const promiseOrCallback = (promise, callback) => { if (typeof callback != "function") { return promise; } return promise .then((result) => (callback(null, result))) .catch(callback); }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ const promiseOrCallback = function(func, callback) { return new Promise(async (resolve, reject) => { try { const data = await func(); if (typeof callback == "function") { return callback(null, data); } return resolve(data); } catch (err) { if (typeof callback == "function") { return callback(err, null); } return reject(err); } }); };