How to use the coroutine function from bluebird

Find comprehensive JavaScript bluebird.coroutine code examples handpicked from public code repositorys.

Bluebird.coroutine is a function that transforms an asynchronous function into a generator-based function that can be paused and resumed using yield statements.

3565
3566
3567
3568
3569
3570
3571
3572
3573
3574

var result;
if (!implementsReturn) {
    var reason = new Promise.CancellationError(
        "generator .return() sentinel");
    Promise.coroutine.returnSentinel = reason;
    this._promise._attachExtraTrace(reason);
    this._promise._pushContext();
    result = tryCatch(this._generator["throw"]).call(this._generator,
                                                     reason);
fork icon3
star icon16
watch icon0

242
243
244
245
246
247
248
249
250
251

getTlsOptions() {
  return this.httpsOptions;
},

validateAndPreprocessConfig: Promise.coroutine(function *validateAndPreprocessConfig(config) {
  let canRun = false;
  if (config.http && config.http.port) {
    const uniqueIps = yield util.uniqueIps(config.http.ipAddresses);
    if (uniqueIps.length > 0) {
fork icon32
star icon5
watch icon8

+ 11 other calls in file

How does bluebird.coroutine work?

Bluebird.coroutine works by taking an asynchronous function and wrapping it with a generator-based function that allows the function to be paused and resumed using yield statements. When a function is wrapped using Bluebird.coroutine, it returns a generator object that can be used to execute the function. The generator object has a next method that starts the execution of the function and returns a promise that resolves to an object with two properties: value and done. The value property contains the result of the current yield statement and the done property is a boolean that indicates whether the function has finished executing. When the next method is called again, the function resumes execution from the point where it was paused and continues until the next yield statement is encountered. This process continues until the function is completely executed, at which point the done property is set to true. Bluebird.coroutine also provides additional functionality for error handling, such as automatically rejecting the promise if an error is thrown within the generator function.

2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
},

injectPluginRouter() {
  this.expressApp.use(this.pluginRouter);
},
installPlugin: Promise.coroutine(function*(pluginContext) {
  const plugin = pluginContext.pluginDef;
  const urlBase = zLuxUrl.makePluginURL(this.options.productCode, 
      plugin.identifier);
  const nodeContext = pluginContext.server.config.user.node;
fork icon32
star icon5
watch icon0

+ 2 other calls in file

115
116
117
118
119
120
121
122
123
124
125
126
};


/**
 * Run the queue. Handle the rear message.
 */
Network.prototype.run = Promise.coroutine(function* () {
  if (!this._queue.length)
    return;


  var message = this._queue.pop();
fork icon23
star icon17
watch icon4

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const Promise = require("bluebird");

// Asynchronous function that returns a promise
function asyncFunction() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Async function resolved");
    }, 1000);
  });
}

// Wrap the asynchronous function with bluebird.coroutine
const generatorFunction = Promise.coroutine(function* () {
  const result = yield asyncFunction();
  console.log(result);
});

// Execute the generator function
generatorFunction();

In this example, we have an asynchronous function called asyncFunction that returns a Promise that resolves after a delay of 1 second. We then wrap this function using Promise.coroutine, which returns a generator function called generatorFunction. Inside generatorFunction, we use the yield keyword to pause the execution of the function and wait for the Promise returned by asyncFunction to resolve. Once the Promise is resolved, the value of the result variable is set to the resolved value of the Promise, and we log the result to the console. Finally, we call the generatorFunction to start the execution of the wrapped asynchronous function. When the yield statement is encountered, the execution of the function is paused until the Promise resolves, at which point the value of result is set and logged to the console.

47
48
49
50
51
52
53
54
55
56
57
//
// Mocha "helpers" to support coroutines tests
//
var Bluebird = require('bluebird')


global.before_ = function (f) { before ( Bluebird.coroutine(f) ) }
global.beforeEach_ = function (f) { beforeEach ( Bluebird.coroutine(f) ) }
global.it_ = function (description, f) { it ( description, Bluebird.coroutine(f) ) }
global.xit_ = function (description, f) { xit ( description, f ) }
global.it_.only = function (description, f) { it.only( description, Bluebird.coroutine(f) ) }
fork icon8
star icon1
watch icon0

+ 3 other calls in file

2
3
4
5
6
7
8
9
10
11
12
13
const Promise = require('bluebird');
const sessionManager = require('./mistifly-proxy');
const config = require('./config.json');


const search = function () {
    return Promise.coroutine(function *(){
        const client = yield sessionManager.createClient;


        const session = yield sessionManager.createSessionPromise(client);

fork icon0
star icon1
watch icon0

28
29
30
31
32
33
34
35
36
37
38
};


// Wrap express route
// https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/
exports.route = function(genFn) { // 1
    var cr = Promise.coroutine(genFn); // 2
    return function(req, res, next) { // 3
        cr(req, res, next).catch(next); // 4
    };
};
fork icon0
star icon0
watch icon0