-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce Object.ensurePromise
Allows validation of thenable objects
- Loading branch information
Showing
3 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
var safeToString = require('../safe-to-string') | ||
, isPromise = require('./is-promise'); | ||
|
||
module.exports = function (value) { | ||
if (!isPromise(value)) throw new TypeError(safeToString(value) + " is not a thenable"); | ||
return value; | ||
}; |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
var promise; | ||
a.throws(function () { t(); }, TypeError); | ||
a.throws(function () { t(null); }, TypeError); | ||
a.throws(function () { t("promise"); }, TypeError); | ||
a.throws(function () { t({}); }, TypeError); | ||
a.throws(function () { t(function () {}); }, TypeError); | ||
a.throws(function () { t({ then: {} }); }, TypeError); | ||
promise = { then: function () {} }; | ||
a(t(promise), promise); | ||
promise = function () {}; | ||
promise.then = {}; | ||
a.throws(function () { t(promise); }, TypeError); | ||
promise.then = function () {}; | ||
a(t(promise), promise); | ||
}; |