Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from iov-one/wait
[iov-cli] Add wait() function to repl
- Loading branch information
Showing
5 changed files
with
39 additions
and
10 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
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
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,20 @@ | ||
// This is an example of the non-trivial art of converting an asynchonous | ||
// method into a synchonous one. This should only be used in the REPL where | ||
// await is not available since the user is working in a top level scope. | ||
// | ||
// yarn run ts-node wait-demo.ts | ||
|
||
function wait<T>(promise: Promise<T>): T { | ||
return require('deasync2').await(promise); | ||
} | ||
|
||
const somePromise = new Promise((resolve, _) => { | ||
setTimeout(() => { | ||
console.log("done! Will resolve now"); | ||
resolve("abc"); | ||
}, 2000); | ||
}) | ||
|
||
console.log("Let's wait until we get what we need …"); | ||
const value = wait(somePromise) | ||
console.log(value) |
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