Created
May 31, 2018 16:58
-
-
Save varokas/87d0a4ffdeb344c97d50c5b834c78c18 to your computer and use it in GitHub Desktop.
async and promise
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
//Mock card object | |
let card = { | |
issueCommand: new Promise(function(resolve, reject) { | |
resolve("issueCommand"); | |
}) | |
} | |
//Mock ResponseApdu | |
class ResponseApdu { | |
constructor(rawResponse) { | |
this.rawResponse = rawResponse; | |
} | |
} | |
//Option 1: Use promise (similar to issueCommand: https://github.com/tomkp/smartcard/blob/2373b010790aa11cf8e9df740c5dd1368a0aae25/src/Iso7816Application.js#L38-L63 ) | |
let runCommandPromise = (card) => { | |
return card.issueCommand.then( rawResponse => new ResponseApdu(rawResponse) ) | |
} | |
runCommandPromise(card).then(response => console.log(response)) | |
//Option 2: Declare function itself as async (which is interchangable with Promise) | |
let runCommandAsync = async (card) => { | |
let rawResponse = await card.issueCommand; | |
return new ResponseApdu(rawResponse); | |
} | |
runCommandAsync(card).then(response => console.log(response)); //Use as Promise | |
//Or use as async function in someother function | |
async function someOtherFn(card) { | |
return await runCommandAsync(card); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment