Skip to content

Commit

Permalink
feat: Add promise.asCallback method
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 16, 2018
1 parent 66481c0 commit dcc1451
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
19 changes: 19 additions & 0 deletions promise/#/as-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

var ensurePlainFunction = require("../../object/ensure-plain-function")
, ensureThenable = require("../../object/ensure-thenable")
, microtaskDelay = require("../../function/#/microtask-delay");

module.exports = function (callback) {
ensureThenable(this);
ensurePlainFunction(callback);
// Rely on microtaskDelay to escape eventual error swallowing
this.then(
microtaskDelay.call(function (value) {
callback(null, value);
}),
microtaskDelay.call(function (reason) {
callback(reason);
})
);
};
5 changes: 5 additions & 0 deletions promise/#/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

module.exports = {
asCallback: require("./as-callback")
};
3 changes: 2 additions & 1 deletion promise/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

module.exports = {
lazy: require("./lazy")
"#": require("./#"),
"lazy": require("./lazy")
};
32 changes: 32 additions & 0 deletions test/promise/#/as-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

module.exports = function (t, a) {
if (typeof Promise !== "function") return null;
return {
Success: function (d) {
t.call(
new Promise(function (resolve) {
resolve("foo");
}),
function (error, value) {
a(error, null);
a(value, "foo");
d();
}
);
},
Failure: function (d) {
var error = new Error("Rejection");
t.call(
new Promise(function (resolve, reject) {
reject(error);
}),
function (passedError, value) {
a(passedError, error);
a(value, undefined);
d();
}
);
}
};
};
2 changes: 1 addition & 1 deletion test/promise/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"globals": { "setTimeout": true }
"globals": { "Promise": true, "setTimeout": true }
}

0 comments on commit dcc1451

Please sign in to comment.