Skip to content

Instantly share code, notes, and snippets.

@darrentorpey
Last active April 29, 2017 17:46
Show Gist options
  • Save darrentorpey/2a2f50120c0cf3da68b95a9a1e807a61 to your computer and use it in GitHub Desktop.
Save darrentorpey/2a2f50120c0cf3da68b95a9a1e807a61 to your computer and use it in GitHub Desktop.
async vs. non comparison (ES2017)
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