147 lines
3.8 KiB
JavaScript
147 lines
3.8 KiB
JavaScript
"use strict";
|
|
|
|
var adapter = global.adapter;
|
|
var fulfilled = adapter.fulfilled;
|
|
var rejected = adapter.rejected;
|
|
var pending = adapter.pending;
|
|
|
|
var other = { other: "other" }; // a value we don't want to be strict equal to
|
|
|
|
exports.fulfilled = {
|
|
"a synchronously-fulfilled custom thenable": function (value) {
|
|
return {
|
|
then: function (onFulfilled) {
|
|
onFulfilled(value);
|
|
}
|
|
};
|
|
},
|
|
|
|
"an asynchronously-fulfilled custom thenable": function (value) {
|
|
return {
|
|
then: function (onFulfilled) {
|
|
setTimeout(function () {
|
|
onFulfilled(value);
|
|
}, 1);
|
|
}
|
|
};
|
|
},
|
|
|
|
"a synchronously-fulfilled one-time thenable": function (value) {
|
|
var numberOfTimesThenRetrieved = 0;
|
|
var ret = Object.create(null, {
|
|
then: {
|
|
get: function () {
|
|
|
|
if (numberOfTimesThenRetrieved === 0) {
|
|
++numberOfTimesThenRetrieved;
|
|
return function (onFulfilled) {
|
|
onFulfilled(value);
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
});
|
|
return ret;
|
|
},
|
|
|
|
"a thenable that tries to fulfill twice": function (value) {
|
|
return {
|
|
then: function (onFulfilled) {
|
|
onFulfilled(value);
|
|
onFulfilled(other);
|
|
}
|
|
};
|
|
},
|
|
|
|
"a thenable that fulfills but then throws": function (value) {
|
|
return {
|
|
then: function (onFulfilled) {
|
|
onFulfilled(value);
|
|
throw other;
|
|
}
|
|
};
|
|
},
|
|
|
|
"an already-fulfilled promise": function (value) {
|
|
return fulfilled(value);
|
|
},
|
|
|
|
"an eventually-fulfilled promise": function (value) {
|
|
var tuple = pending();
|
|
setTimeout(function () {
|
|
tuple.fulfill(value);
|
|
}, 1);
|
|
return tuple.promise;
|
|
}
|
|
};
|
|
|
|
exports.rejected = {
|
|
"a synchronously-rejected custom thenable": function (reason) {
|
|
return {
|
|
then: function (onFulfilled, onRejected) {
|
|
onRejected(reason);
|
|
}
|
|
};
|
|
},
|
|
|
|
"an asynchronously-rejected custom thenable": function (reason) {
|
|
return {
|
|
then: function (onFulfilled, onRejected) {
|
|
setTimeout(function () {
|
|
onRejected(reason);
|
|
}, 1);
|
|
}
|
|
};
|
|
},
|
|
|
|
"a synchronously-rejected one-time thenable": function (reason) {
|
|
var numberOfTimesThenRetrieved = 0;
|
|
return Object.create(null, {
|
|
then: {
|
|
get: function () {
|
|
if (numberOfTimesThenRetrieved === 0) {
|
|
++numberOfTimesThenRetrieved;
|
|
return function (onFulfilled, onRejected) {
|
|
onRejected(reason);
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
"a thenable that immediately throws in `then`": function (reason) {
|
|
return {
|
|
then: function () {
|
|
throw reason;
|
|
}
|
|
};
|
|
},
|
|
|
|
"an object with a throwing `then` accessor": function (reason) {
|
|
return Object.create(null, {
|
|
then: {
|
|
get: function () {
|
|
throw reason;
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
"an already-rejected promise": function (reason) {
|
|
var ret = rejected(reason);
|
|
ret.caught(function(){});
|
|
return ret;
|
|
},
|
|
|
|
"an eventually-rejected promise": function (reason) {
|
|
var tuple = pending();
|
|
setTimeout(function () {
|
|
tuple.reject(reason);
|
|
}, 1);
|
|
return tuple.promise;
|
|
}
|
|
};
|