Merge pull request #260 from lehni/feature/async-boot-scripts-2.x
Do not call callbacks twice in async boot scripts
This commit is contained in:
commit
ded190ee8a
|
@ -306,11 +306,11 @@ function runScripts(app, list, callback) {
|
||||||
async.eachSeries(functions, function(f, done) {
|
async.eachSeries(functions, function(f, done) {
|
||||||
debug('Running script %s', f.path);
|
debug('Running script %s', f.path);
|
||||||
var cb = function(err) {
|
var cb = function(err) {
|
||||||
debug('Async function finished %s', f.path);
|
debug('Async function %s %s', err ? 'failed' : 'finished', f.path);
|
||||||
done(err);
|
done(err);
|
||||||
// Make sure done() isn't called twice, e.g. if a script returns a
|
// Make sure done() isn't called twice, e.g. if a script returns a
|
||||||
// thenable object and also calls the passed callback.
|
// thenable object and also calls the passed callback.
|
||||||
cb = null;
|
cb = function() {};
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
var result = f.func(app, cb);
|
var result = f.func(app, cb);
|
||||||
|
|
|
@ -373,6 +373,23 @@ describe('executor', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('with boot script returning a promise and calling callback',
|
||||||
|
function() {
|
||||||
|
before(function() {
|
||||||
|
process.promiseAndCallback = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function() {
|
||||||
|
delete process.promiseAndCallback;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should only call the callback once', function(done) {
|
||||||
|
// Note: Mocha will fail this test if done() is called twice
|
||||||
|
boot.execute(app, simpleAppInstructions(), done);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
describe('for mixins', function() {
|
describe('for mixins', function() {
|
||||||
var options;
|
var options;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
// Copyright IBM Corp. 2017. All Rights Reserved.
|
||||||
|
// Node module: loopback-boot
|
||||||
|
// This file is licensed under the MIT License.
|
||||||
|
// License text available at https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
var Promise = require('bluebird');
|
||||||
|
|
||||||
|
module.exports = function(app, callback) {
|
||||||
|
callback();
|
||||||
|
if (process.promiseAndCallback) {
|
||||||
|
return Promise.reject();
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue