mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
7 years ago
11 changed files with 431 additions and 0 deletions
@ -0,0 +1,21 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
Object.prototype.then |
|||
Object.prototype.then |
|||
===*/ |
|||
|
|||
// The .then() function can be inherited. Here, setting it in Object.prototype
|
|||
// makes every object value thenable.
|
|||
var O1 = {}; |
|||
var O2 = {}; |
|||
Object.prototype.then = function () { print('Object.prototype.then'); }; |
|||
Promise.resolve().then(function () { return O1; }); |
|||
Promise.resolve().then(function () { return O2; }); |
|||
|
|||
print('done'); |
@ -0,0 +1,23 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
O1.then |
|||
===*/ |
|||
|
|||
// Thenable objects are detected purely by duck typing: if the resolve value is
|
|||
// an object with a callable .then(), it's treated as a thenable. The .then()
|
|||
// property may be own or inherited.
|
|||
|
|||
var O1 = { then: function () { print('O1.then'); } }; |
|||
var O2 = { them: function () { print('O1.them'); } }; |
|||
var O3 = { then: 123 }; |
|||
Promise.resolve().then(function () { return O1; }); |
|||
Promise.resolve().then(function () { return O2; }); |
|||
Promise.resolve().then(function () { return O3; }); |
|||
|
|||
print('done'); |
@ -0,0 +1,37 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
create promise |
|||
call .then() |
|||
done |
|||
return thenable |
|||
thenable.then() called |
|||
2 function function |
|||
reject: 123 |
|||
===*/ |
|||
|
|||
// Thenable .then() is executed via the job queue.
|
|||
|
|||
print('create promise'); |
|||
var P = Promise.resolve().then(function () { |
|||
print('return thenable'); |
|||
return { |
|||
then: function (resolve, reject) { |
|||
print('thenable.then() called'); |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
reject(123); |
|||
} |
|||
}; |
|||
}); |
|||
print('call .then()'); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,37 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
create promise |
|||
executor called, resolve with thenable |
|||
call .then() |
|||
done |
|||
thenable.then() called |
|||
2 function function |
|||
reject: 123 |
|||
===*/ |
|||
|
|||
// Thenable .then() is executed via the job queue.
|
|||
|
|||
print('create promise'); |
|||
var P = new Promise(function (resolve, reject) { |
|||
print('executor called, resolve with thenable'); |
|||
resolve({ |
|||
then: function (resolve, reject) { |
|||
print('thenable.then() called'); |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
reject(123); |
|||
} |
|||
}); |
|||
}); |
|||
print('call .then()'); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,29 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
reject: 123 |
|||
===*/ |
|||
|
|||
// Basic thenable case, rejection.
|
|||
|
|||
var P = Promise.resolve().then(function () { |
|||
return { |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
reject(123); |
|||
} |
|||
}; |
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,46 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
reject: Error: aiee |
|||
false |
|||
false |
|||
===*/ |
|||
|
|||
// Thenable is rejected, check that other resolve/reject calls are no-op.
|
|||
|
|||
var resolve1, reject1; |
|||
var resolve2, reject2; |
|||
var P = new Promise(function (resolve, reject) { |
|||
resolve1 = resolve; |
|||
reject1 = reject; |
|||
|
|||
resolve1({ |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve2 = resolve; |
|||
reject2 = reject; |
|||
reject2(new Error('aiee')); |
|||
resolve2(321); // nop
|
|||
reject2(new Error('barf')); // nop
|
|||
throw 123; // ignored because already resolved
|
|||
} |
|||
}); |
|||
reject1(new Error('aiee')); // nop
|
|||
resolve1(123); // nop
|
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', String(e)); |
|||
}).then(function () { |
|||
print(resolve1 === resolve2); |
|||
print(reject1 === reject2); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,29 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
reject: 123 |
|||
===*/ |
|||
|
|||
// Error thrown by thenable.then() is mapped to rejection.
|
|||
|
|||
var P = Promise.resolve().then(function () { |
|||
return { |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
throw 123; |
|||
} |
|||
}; |
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,72 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
2 function function |
|||
false |
|||
false |
|||
reject: Error: aiee |
|||
false |
|||
false |
|||
false |
|||
false |
|||
false |
|||
false |
|||
===*/ |
|||
|
|||
// Two level thenable with rejection, with neutralization checks.
|
|||
|
|||
var resolve1, reject1; |
|||
var resolve2, reject2; |
|||
var resolve3, reject3; |
|||
var P = new Promise(function (resolve, reject) { |
|||
resolve1 = resolve; |
|||
reject1 = reject; |
|||
|
|||
resolve1({ |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve2 = resolve; |
|||
reject2 = reject; |
|||
resolve2({ |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve3 = resolve; |
|||
reject3 = reject; |
|||
|
|||
print(resolve2 === resolve3); |
|||
print(reject2 === reject3); |
|||
|
|||
resolve1('aiee'); // neutralized already
|
|||
resolve2('aiee'); // neutralized already
|
|||
|
|||
reject3(new Error('aiee')); |
|||
resolve3(321); |
|||
reject3(new Error('barf')); |
|||
throw 123; // ignored because already resolved
|
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
reject1(new Error('aiee')); |
|||
resolve1(123); |
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', String(e)); |
|||
}).then(function () { |
|||
print(resolve1 === resolve2); |
|||
print(reject1 === reject2) |
|||
print(resolve1 === resolve3); |
|||
print(reject1 === reject3); |
|||
print(resolve2 === resolve3); |
|||
print(reject2 === reject3); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,30 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
fulfill: 123 |
|||
===*/ |
|||
|
|||
// Basic thenable: Promise is resolved with a plain object with a callable
|
|||
// .then value.
|
|||
|
|||
var P = Promise.resolve().then(function () { |
|||
return { |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve(123); |
|||
} |
|||
}; |
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,49 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
fulfill: 123 |
|||
false |
|||
false |
|||
===*/ |
|||
|
|||
// Thenable, which first calls resolve() on the fresh resolve/reject pair.
|
|||
// Calls to previous resolve/reject or calling the new resolve/reject again
|
|||
// are no-op. Also verify that a fresh resolve/reject function pair is
|
|||
// created when the initial Promise is resolved with a thenable.
|
|||
|
|||
var resolve1, reject1; |
|||
var resolve2, reject2; |
|||
var P = new Promise(function (resolve, reject) { |
|||
resolve1 = resolve; |
|||
reject1 = reject; |
|||
|
|||
resolve1({ |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve2 = resolve; |
|||
reject2 = reject; |
|||
resolve2(123); |
|||
reject2(new Error('aiee')); |
|||
resolve2(321); |
|||
throw 123; // ignored because already resolved
|
|||
} |
|||
}); |
|||
reject1(new Error('aiee')); |
|||
resolve1(123); |
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}).then(function () { |
|||
print(resolve1 === resolve2); |
|||
print(reject1 === reject2); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,58 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
2 function function |
|||
2 function function |
|||
fulfill: 123 |
|||
false |
|||
false |
|||
false |
|||
false |
|||
===*/ |
|||
|
|||
// Two-level thenable, resolve/reject reuse checks.
|
|||
var resolve1, reject1; |
|||
var resolve2, reject2; |
|||
var resolve3, reject3; |
|||
var P = new Promise(function (resolve, reject) { |
|||
resolve1 = resolve; |
|||
reject1 = reject; |
|||
|
|||
resolve1({ |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve2 = resolve; |
|||
reject2 = reject; |
|||
resolve2({ |
|||
then: function (resolve, reject) { |
|||
print(arguments.length, typeof resolve, typeof reject); |
|||
resolve3 = resolve; |
|||
reject3 = reject; |
|||
resolve3(123); |
|||
reject3(new Error('aiee')); |
|||
resolve3(321); |
|||
throw 123; // ignored because already resolved
|
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
reject1(new Error('aiee')); |
|||
resolve1(123); |
|||
}); |
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}).then(function () { |
|||
print(resolve1 === resolve2); |
|||
print(reject1 === reject2); |
|||
print(resolve2 === resolve3); |
|||
print(reject2 === reject3); |
|||
}); |
|||
|
|||
print('done'); |
Loading…
Reference in new issue