NodeJs - 等待来自异步方法的承诺解决。
NodeJs - Wait for a promise resolution from an asyncronous method.
我已经编写了一个验证库,我希望能够在 Node 中同步和异步调用它。我尝试使用 async/co 的承诺,但我
哪儿也去不了。有没有人对如何使此代码同步有很好的建议?我试图从验证工厂调用它,返回的查找正在异步地到达数据库。
var lookupFactory = require('../lookup/lookupFactory');
var Promise = require("bluebird");
var async = require('async');
exports.validate = function (required, field, errors) {
if (required && field.value == null) {
errors.push('Field is required');
return;
}
var lookupType = field.validation.lookupType;
var s = lookupFactory.lookup(lookupType);
s.validate('12', function (err, data) {
if (data.length != 1) {
errors.push('Field value is not valid');
}
});
};
我喜欢承诺。
exports.validate = function (required, field, errors) {
return new Promise(function(fulfill) {
if (required && field.value == null) {
errors.push('Field is required');
return;
}
var lookupType = field.validation.lookupType;
var s = lookupFactory.lookup(lookupType);
s.validate('12', function (err, data) {
if (data.length != 1) {
errors.push('Field value is not valid');
}
fulfill(null /*Argument passed to the other function*/); //Is it here the code end?
});
});
}
exports.validate = function (required, field) {
var lookupType = field.validation.lookupType;
var s = lookupFactory.lookup(lookupType);
return new Promise(function(resolve, reject) {
if (required && field.value == null) {
return reject('Field is required');
}
s.validate('12', function (err, data) {
if(err) {
return reject(err);
} else if (data.length != 1) {
return reject('Field value is not valid');
}
resolve(data);
});
});
}
用法示例:
validate(true, 'name')
.then(function(data) { console.log('Validation succeeded:', data); },
function(reason) { console.log('Validation failed:', reason); });
我已经编写了一个验证库,我希望能够在 Node 中同步和异步调用它。我尝试使用 async/co 的承诺,但我 哪儿也去不了。有没有人对如何使此代码同步有很好的建议?我试图从验证工厂调用它,返回的查找正在异步地到达数据库。
var lookupFactory = require('../lookup/lookupFactory');
var Promise = require("bluebird");
var async = require('async');
exports.validate = function (required, field, errors) {
if (required && field.value == null) {
errors.push('Field is required');
return;
}
var lookupType = field.validation.lookupType;
var s = lookupFactory.lookup(lookupType);
s.validate('12', function (err, data) {
if (data.length != 1) {
errors.push('Field value is not valid');
}
});
};
我喜欢承诺。
exports.validate = function (required, field, errors) {
return new Promise(function(fulfill) {
if (required && field.value == null) {
errors.push('Field is required');
return;
}
var lookupType = field.validation.lookupType;
var s = lookupFactory.lookup(lookupType);
s.validate('12', function (err, data) {
if (data.length != 1) {
errors.push('Field value is not valid');
}
fulfill(null /*Argument passed to the other function*/); //Is it here the code end?
});
});
}
exports.validate = function (required, field) {
var lookupType = field.validation.lookupType;
var s = lookupFactory.lookup(lookupType);
return new Promise(function(resolve, reject) {
if (required && field.value == null) {
return reject('Field is required');
}
s.validate('12', function (err, data) {
if(err) {
return reject(err);
} else if (data.length != 1) {
return reject('Field value is not valid');
}
resolve(data);
});
});
}
用法示例:
validate(true, 'name')
.then(function(data) { console.log('Validation succeeded:', data); },
function(reason) { console.log('Validation failed:', reason); });