Skip to content

Instantly share code, notes, and snippets.

@getify
Created February 14, 2019 20:55
Show Gist options
  • Select an option

  • Save getify/1398f3ce0705ceff1ffb6bb757169ab9 to your computer and use it in GitHub Desktop.

Select an option

Save getify/1398f3ce0705ceff1ffb6bb757169ab9 to your computer and use it in GitHub Desktop.

Revisions

  1. getify created this gist Feb 14, 2019.
    24 changes: 24 additions & 0 deletions 1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function somethingThatReturnsAPromise(x,y,z) { .. }

    function myCallback(err,v) {
    if (err) {
    console.error(err);
    }
    else {
    console.log(v);
    }
    }

    // **************

    somethingThatReturnsAPromise(1,2,3)
    .then( ...thenify(myCallback) );

    somethingThatReturnsAPromise(4,5,6)
    .then( thenify(myCallback)[0] )
    .catch( thenify(myCallback)[1] );

    var [tf,cf] = thenify(myCallback);
    somethingThatReturnsAPromise(7,8,9)
    .then(tf)
    .catch(cf);
    10 changes: 10 additions & 0 deletions 2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    function thenify(errFirstCB) {
    return [
    function tf(v) {
    errFirstCB.call(this,undefined,v);
    },
    function cf(e) {
    errFirstCB.call(this,e);
    }
    ];
    }