[njs] Promise: fixed the catch handler for Promise.prototype.finally().

Alexander Borisov alexander.borisov at nginx.com
Tue Nov 3 17:17:33 UTC 2020


details:   https://hg.nginx.org/njs/rev/c791e3943df1
branches:  
changeset: 1558:c791e3943df1
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Tue Nov 03 20:14:33 2020 +0300
description:
Promise: fixed the catch handler for Promise.prototype.finally().

By spec, the catch handler for the .finally() should always return an
exception.

The issue was introduced in 61bf7a31e685.

diffstat:

 src/njs_promise.c                           |  6 +++++-
 test/js/promise_then_throw_finally_catch.js |  4 ++++
 test/njs_expect_test.exp                    |  3 +++
 3 files changed, 12 insertions(+), 1 deletions(-)

diffs (35 lines):

diff -r b523bbbd8e6d -r c791e3943df1 src/njs_promise.c
--- a/src/njs_promise.c	Sat Oct 31 23:00:03 2020 +0300
+++ b/src/njs_promise.c	Tue Nov 03 20:14:33 2020 +0300
@@ -1023,7 +1023,11 @@ static njs_int_t
 njs_promise_catch_finally_function(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused)
 {
-    return njs_promise_then_finally_function(vm, args, nargs, unused);
+    (void) njs_promise_then_finally_function(vm, args, nargs, unused);
+
+    njs_vm_retval_set(vm, njs_arg(args, nargs, 1));
+
+    return NJS_ERROR;
 }
 
 
diff -r b523bbbd8e6d -r c791e3943df1 test/js/promise_then_throw_finally_catch.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/promise_then_throw_finally_catch.js	Tue Nov 03 20:14:33 2020 +0300
@@ -0,0 +1,4 @@
+Promise.resolve()
+.then(() => {nonExsisting()})
+.finally(() => {})
+.catch(() => {console.log("Done")});
\ No newline at end of file
diff -r b523bbbd8e6d -r c791e3943df1 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Sat Oct 31 23:00:03 2020 +0300
+++ b/test/njs_expect_test.exp	Tue Nov 03 20:14:33 2020 +0300
@@ -1044,3 +1044,6 @@ njs_run {"./test/js/fs_promises_008.js"}
 
 njs_run {"./test/js/fs_promises_009.js"} \
 "test recursive fs.rmdirSync"
+
+njs_run {"./test/js/promise_then_throw_finally_catch.js"} \
+"Done"


More information about the nginx-devel mailing list