From e2def5f88bc398cefc051e52aea614d32034fdec Mon Sep 17 00:00:00 2001 From: Radium Zheng Date: Wed, 1 Aug 2018 11:19:47 +1000 Subject: [PATCH] simplify Promise chaining in FlacAdapter --- .../recording/flac/FlacAdapter.js | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/react/features/local-recording/recording/flac/FlacAdapter.js b/react/features/local-recording/recording/flac/FlacAdapter.js index 804e970b2..c8de2ee3c 100644 --- a/react/features/local-recording/recording/flac/FlacAdapter.js +++ b/react/features/local-recording/recording/flac/FlacAdapter.js @@ -145,25 +145,20 @@ export class FlacAdapter extends RecordingAdapter { /** * Replaces the current microphone MediaStream. * - * @param {*} micDeviceId - New microphone ID. + * @param {string} micDeviceId - New microphone ID. * @returns {Promise} */ _replaceMic(micDeviceId) { if (this._audioContext && this._audioProcessingNode) { - return new Promise((resolve, reject) => { - this._getAudioStream(micDeviceId).then(newStream => { - const newSource = this._audioContext - .createMediaStreamSource(newStream); + return this._getAudioStream(micDeviceId).then(newStream => { + const newSource = this._audioContext + .createMediaStreamSource(newStream); + + this._audioSource.disconnect(); + newSource.connect(this._audioProcessingNode); + this._stream = newStream; + this._audioSource = newSource; - this._audioSource.disconnect(); - newSource.connect(this._audioProcessingNode); - this._stream = newStream; - this._audioSource = newSource; - resolve(); - }) - .catch(() => { - reject(); - }); }); } @@ -221,7 +216,7 @@ export class FlacAdapter extends RecordingAdapter { }); }); - const callbackInitAudioContext = (resolve, reject) => { + const callbackInitAudioContext = () => this._getAudioStream(micDeviceId) .then(stream => { this._stream = stream; @@ -244,19 +239,18 @@ export class FlacAdapter extends RecordingAdapter { }); }; logger.debug('AudioContext is set up.'); - resolve(); }) .catch(err => { logger.error(`Error calling getUserMedia(): ${err}`); - reject(); + + return Promise.reject(err); }); - }; // Because Promise constructor immediately executes the executor // function. This is undesirable, we want callbackInitAudioContext to be // executed only **after** promiseInitWorker is resolved. return promiseInitWorker - .then(() => new Promise(callbackInitAudioContext)); + .then(callbackInitAudioContext); } /**