[英]How can I pass the value of a Promise to another Function?
I'm a little new to Javascript, and am having a hard time with the asynchronous aspect of it. 我对Java有点陌生,但是在异步方面却很难。 My program checks values of two objects, where the second object doesn't have a vital property I need in order to complete the check. 我的程序检查两个对象的值,其中第二个对象没有我需要的重要属性才能完成检查。 So I made a promise to get that value/property (the ID), and now I need to pass that ID value along to a check function. 因此,我承诺要获得该值/属性(ID),现在我需要将该ID值传递给检查函数。 The check function should simply return a true/false to see if the ID's match. 检查函数应仅返回true / false,以查看ID是否匹配。 The value of the check function is passed to another function which then acts appropriately and edits the thing if necessary. 检查函数的值将传递给另一个函数,然后该函数将适当地执行操作,并在必要时进行编辑。 So I basically can't access the value of tick outside it's brackets. 因此,我基本上无法访问方括号之外的tick值。 I've included the snippet of my code where all of this is happening, as all of this is easier to visualize with it. 我已经在所有代码中包含了发生的所有代码片段,因为使用它们可以更轻松地将它们可视化。 Can someone provide me with a solution to this issue? 有人可以为我提供解决方案吗? Any advice would help immensely! 任何建议将极大地帮助您! I want to minimize the modification of the script as much as possible. 我想尽可能地减少对脚本的修改。
var Q = require('q');
getID = function(instance, person, callback){
var = deferred = Q.defer();
var url = 'www.blah.com';
var options = {
'url': url
};
request.get(options, function(error, response, body){
if (error) {
deferred.reject(error);
}else{
var res = body;
var obj = JSON.parse(res);
var id = obj.id;
deferred.resolve(id);
} else deferred(obj);
});
check = function(instance, thing1, thing2){
var tick = true;
getID(instance, thing2).then(function(id)){
var id_1 = thing1.id; // thing1 passed into check with ID
var id_2 = thing2.id; // thing 2 now has id attached to it
if( id_1 == id_2 ){
tick = true; // VALUE 1
}else{
tick = false; // VALUE 2
});
// NEED VALUE 1 OR 2 OF TICK HERE
if(thing1.name == thing2.name){
tick = true;
else{
tick = false;
}
// similar checks to name but with ADDRESS, EMAIL, PHONE NUMBER
// these properties are already appended to thing1 and thing 2 so no need to call for them
};
editThing = function(instance, thing, callback){
var checked = check(instance, thing1, thing2);
if(checked){
// edit thing
}else{
// don't edit thing
};
Since you're making a promise of work to be done, and you need output from that work, you'll need pass that promise along to the code who's wanting the final output. 由于您承诺要完成的工作,并且需要该工作的输出,因此您需要将该承诺传递给想要最终输出的代码。
I'm not going to try to rewrite the code from your post, so allow me to paraphrase: 我不会尝试重写您的帖子中的代码,因此请允许我解释一下:
getThing = function(thing){
var deferred = Q.defer();
...
request.get(options, function(error, response, body){
if (error) {
deferred.reject(error);
} else {
...
deferred.resolve(thingMadeFromResponse);
}
});
return deferred;
}
check = function(thingWeHave, thingWeNeedFetched){
return getThing(thingWeNeedFetched).then(function(thingWeFetched)){
// check logic
checked = thingWeHave.id == thingWeFetched.id;
...
return checked;
});
};
editThing = function(instance, thing, callback){
check(thingWeHave, thingWeNeedFetched).then(function(checked) {
if(checked){
// edit thing
}else{
// don't edit thing
}
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.