-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasm] Make runtime_is_initialized promise callbacks one-shot (#70694)
* [wasm] Make runtime_is_initialized promise callbacks one-shot Throw if runtime_is_initialized_resolve or runtime_is_initialized_reject is called more than once * Add a slightly generalized GuardedPromise<T> object Protects against multiple-resolve, multiple-reject, reject after resolve and resolve after reject. Does not protect against the executor throwing.
- Loading branch information
1 parent
3428247
commit fb9be0a
Showing
2 changed files
with
38 additions
and
3 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,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
/// A Promise<T> that guards against multiple-resolve, multiple-reject, reject-after-accept and accept-after-reject. | ||
class GuardedPromise<T> extends Promise<T> { | ||
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) { | ||
super((resolve, reject) => { | ||
let resolved = false; | ||
let rejected = false; | ||
executor((value: T | PromiseLike<T>) => { | ||
if (resolved) { | ||
throw new Error("Promise resolved more than once"); | ||
} | ||
if (rejected) { | ||
throw new Error("Can not resolve a Promise after it has been rejected"); | ||
} | ||
resolved = true; | ||
resolve(value); | ||
}, (reason: any) => { | ||
if (resolved) { | ||
throw new Error("Can not reject a Promise after it has been resolved"); | ||
} | ||
if (rejected) { | ||
throw new Error("Promise rejected more than once"); | ||
} | ||
rejected = true; | ||
reject(reason); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default GuardedPromise; | ||
|
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