Last active
April 29, 2017 17:46
-
-
Save darrentorpey/2a2f50120c0cf3da68b95a9a1e807a61 to your computer and use it in GitHub Desktop.
async vs. non comparison (ES2017)
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
function newDecodedSourceSync(audioData) { | |
return new Promise((resolve) => { | |
const audioCtx = new AudioContext(); | |
audioCtx.decodeAudioData(audioData, buffer => { | |
const source = createBufferSource(audioCtx, buffer); | |
resolve([audioCtx, buffer, source]); | |
}, | |
(e) => `Error with decoding audio data ${e.err}` | |
); | |
}); | |
} | |
async function newDecodedSource(audioData) { | |
try { | |
const context = new AudioContext(); | |
const buffer = await context.decodeAudioData(audioData); | |
const source = createBufferSource(context, buffer); | |
return [context, buffer, source]; | |
} catch(e) { | |
throw new Error(`Error with decoding audio data ${e.err}`) | |
} | |
} | |
[newDecodedSource, newDecodedSourceSync].forEach(fn => | |
fn(audioData).then(([context, buffer, source]) => | |
console.log(context, buffer, source))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment