Fix handling of return of rejected promise within a promise

This commit is contained in:
Gordon Williams 2016-12-19 13:38:48 +00:00
parent 0b1dd85687
commit 8191ca4164
3 changed files with 18 additions and 5 deletions

View File

@ -1,4 +1,5 @@
1v91 : Fix recent regression if no Boot Code defined at startup
Fix handling of return of rejected promise within a promise
1v90 : Fixes for Promise.all (passing in non-promises, pre-resolved, and ordering) (fix #976)
Fix arrow function bug when parsing multiple arguments

View File

@ -80,12 +80,15 @@ void _jswrap_promise_resolve_or_reject(JsVar *promise, JsVar *data, JsVar *fn) {
if (chainedPromise) {
if (_jswrap_promise_is_promise(result)) {
// if we were given a promise, loop its 'then' in here
JsVar *fn = jsvNewNativeFunction((void (*)(void))_jswrap_promise_queueresolve, JSWAT_VOID|JSWAT_THIS_ARG|(JSWAT_JSVAR<<JSWAT_BITS));
if (fn) {
jsvObjectSetChild(fn, JSPARSE_FUNCTION_THIS_NAME, chainedPromise);
_jswrap_promise_add(result, fn, true);
jsvUnLock(fn);
JsVar *fnres = jsvNewNativeFunction((void (*)(void))_jswrap_promise_queueresolve, JSWAT_VOID|JSWAT_THIS_ARG|(JSWAT_JSVAR<<JSWAT_BITS));
JsVar *fnrej = jsvNewNativeFunction((void (*)(void))_jswrap_promise_queuereject, JSWAT_VOID|JSWAT_THIS_ARG|(JSWAT_JSVAR<<JSWAT_BITS));
if (fnres && fnrej) {
jsvObjectSetChild(fnres, JSPARSE_FUNCTION_THIS_NAME, chainedPromise);
jsvObjectSetChild(fnrej, JSPARSE_FUNCTION_THIS_NAME, chainedPromise);
_jswrap_promise_add(result, fnres, true);
_jswrap_promise_add(result, fnrej, false);
}
jsvUnLock2(fnres,fnrej);
} else {
_jswrap_promise_queueresolve(chainedPromise, result);
}

9
tests/test_promise7.js Normal file
View File

@ -0,0 +1,9 @@
//http://forum.espruino.com/conversations/297505/#comment13375674
new Promise((x,y)=>x()).then(dev=>{
return new Promise((x,y)=>y("Uh-oh"));
}).then(s=>{
console.log("ok", s);
}).catch(e=>{
console.log("expected error", e);
result = e == "Uh-oh";
});