-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a function that preserves stack when continuing promises around other promises
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 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 |
---|---|---|
@@ -1,2 +1,48 @@ | ||
# q-lite | ||
Lightweight implementation of Q.js for performance | ||
|
||
## Methods | ||
|
||
### Q.safetyAwait | ||
|
||
Solves stack preservation when calling async functions without an await over async boundries | ||
|
||
``` | ||
function testFn(){ | ||
const deferred = Q.defer() | ||
async function a(){ | ||
await Q.delay(1) | ||
throw new Error('test') | ||
} | ||
const promise = a() | ||
// will fail if this becomes awaited | ||
// this is because the stack is not preserved | ||
await Q.delay(10) | ||
const p = Q.cancelledRace([deferred.promise,promise]) | ||
deferred.resolve() | ||
return await p | ||
} | ||
``` | ||
|
||
Stack would be lost (`testFn`) in this test over the `Q.delay` call. However with `Q.safetyAwait` method state can be preserved. | ||
|
||
``` | ||
function testFn(){ | ||
const deferred = Q.defer() | ||
async function a(){ | ||
await Q.delay(1) | ||
throw new Error('test') | ||
} | ||
const promise = a() | ||
// will fail if this becomes awaited | ||
// this is because the stack is not preserved | ||
await Q.safetyAwait([Q.delay(10)], [promise]) | ||
const p = Q.cancelledRace([deferred.promise,promise]) | ||
deferred.resolve() | ||
return await p | ||
} | ||
``` |
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
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