Skip to content

Instantly share code, notes, and snippets.

@shinchit
Created June 23, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinchit/638dce462e75eb30abdab34d22b63a85 to your computer and use it in GitHub Desktop.
Save shinchit/638dce462e75eb30abdab34d22b63a85 to your computer and use it in GitHub Desktop.
import Promise from 'bluebird';
import { Mutex } from 'await-semaphore';
const mutex = new Mutex();
mutex.use(() => {
// write process you want to control exclusively
// example shows below
const promise = new Promise((resolve) => {
Promise.props({ // see. http://bluebirdjs.com/docs/api/promise.props.html
labels: this.getLabels(), // Slow and asynchronous process
feelings: this.getFeelingsData(), // Slow and asynchronous process
})
.then((result) => {
this.labels.replace(result.labels);
this.feelingsData.replace(result.feelings);
resolve(true);
})
.catch((e) => {
throw e;
});
});
return promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment