Skip to content

Instantly share code, notes, and snippets.

@mheap
Last active December 13, 2017 13:46
Show Gist options
  • Save mheap/4abf07d84a7ae08b729d5934d43a4e66 to your computer and use it in GitHub Desktop.
Save mheap/4abf07d84a7ae08b729d5934d43a4e66 to your computer and use it in GitHub Desktop.

Revisions

  1. mheap revised this gist Dec 13, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example-one.js
    Original 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){
    const promise = new Promise((resolve, reject) => {
    return resolve(a + b);
    });

  2. mheap revised this gist Dec 13, 2017. 4 changed files with 26 additions and 24 deletions.
    9 changes: 9 additions & 0 deletions example-one.js
    Original 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);
    17 changes: 17 additions & 0 deletions example-two.js
    Original 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){
    8 changes: 0 additions & 8 deletions one.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +0,0 @@
    const promiseOrCallback = (promise, callback) => {
    if (typeof callback != "function") {
    return promise;
    }
    return promise
    .then((result) => (callback(null, result)))
    .catch(callback);
    };
    16 changes: 0 additions & 16 deletions two.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +0,0 @@
    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);
    }
    });
    };
  3. mheap revised this gist Dec 13, 2017. 2 changed files with 14 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions example-one.js
    Original 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);
    }
    7 changes: 7 additions & 0 deletions example-two.js
    Original 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);
    }
  4. mheap created this gist Dec 13, 2017.
    8 changes: 8 additions & 0 deletions one.js
    Original 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);
    };
    16 changes: 16 additions & 0 deletions two.js
    Original 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);
    }
    });
    };