Javascript 从承诺中返回目标
我对使用承诺有点陌生,在返回对象时遇到了问题。我有以下函数调用SPServices从列表中检索一项。我知道SPSService调用正在返回列表项,但返回的数据未定义:Javascript 从承诺中返回目标,javascript,jquery,promise,spservices,Javascript,Jquery,Promise,Spservices,我对使用承诺有点陌生,在返回对象时遇到了问题。我有以下函数调用SPServices从列表中检索一项。我知道SPSService调用正在返回列表项,但返回的数据未定义: $('.support-View').on('click', function () { var thisID = $(this).data("id"); var message = getOneDetails(thisID); console.log("This Data : " + message);
$('.support-View').on('click', function () {
var thisID = $(this).data("id");
var message = getOneDetails(thisID);
console.log("This Data : " + message); //<-- Comes back undefined
$.when(message).done(function() {
thisMessage = message;
viewRequestDialog(thisID, message);
});
});
$('support View')。在('click',函数(){
var thisID=$(this).data(“id”);
var message=getOneDetails(thisID);
console.log(“此数据:+消息);//您永远无法返回承诺中的值,因为它可能尚未到达。对于根据第一个承诺的结果计算的值,您只能返回另一个承诺。您的第一个代码段需要如下所示:
function getOneDetails (thisID, viewtype) {
var CAML = '<Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">' + thisID + '</Value></Eq></Where></Query>';
var list = 'Support Requests';
var requestsPromise = $().SPServices.SPGetListItemsJson({
listName: list,
CAMLQuery: CAML,
includeAllAttrs: true
});
$.when(requestsPromise).done(function() {
requestData = this.data;
console.log(this.data); //<---- Data is defined here
})
.then(function(requestData) {
console.log(requestData); //<---- But undefined here
return requestData;
});
$('.support-View').on('click', function () {
var thisID = $(this).data("id");
var messagePromise = getOneDetails(thisID);
messagePromise.done(function(message) {
console.log("This Data : " + message); // <-- Comes back as an argument
// to the callback
// thisMessage = message; // Don't ever do this. Global variables are
// useless when you don't know *when* they
// will contain a value. If you need to store
// something, store the `messagePromise`.
viewRequestDialog(thisID, message);
});
});
但你可能需要这样做
return requestsPromise.then(function() { // again, return the result of the call
var requestData = this.data; // take it from wherever (unsure what `this` is)
// ^^^ local variable
console.log(requestData); // <-- Data is defined here
return requestData; // and can be returned to resolve the promise with
});
returnrequestspromise。然后(function(){//再次返回调用的结果
var requestData=this.data;//从任何地方获取它(不确定'this'是什么)
//^^^局部变量
console.log(requestData);//您永远无法返回承诺中的值,因为它可能尚未到达。对于根据第一个承诺的结果计算的值,您只能返回另一个承诺。您的第一个代码段需要如下所示:
function getOneDetails (thisID, viewtype) {
var CAML = '<Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">' + thisID + '</Value></Eq></Where></Query>';
var list = 'Support Requests';
var requestsPromise = $().SPServices.SPGetListItemsJson({
listName: list,
CAMLQuery: CAML,
includeAllAttrs: true
});
$.when(requestsPromise).done(function() {
requestData = this.data;
console.log(this.data); //<---- Data is defined here
})
.then(function(requestData) {
console.log(requestData); //<---- But undefined here
return requestData;
});
$('.support-View').on('click', function () {
var thisID = $(this).data("id");
var messagePromise = getOneDetails(thisID);
messagePromise.done(function(message) {
console.log("This Data : " + message); // <-- Comes back as an argument
// to the callback
// thisMessage = message; // Don't ever do this. Global variables are
// useless when you don't know *when* they
// will contain a value. If you need to store
// something, store the `messagePromise`.
viewRequestDialog(thisID, message);
});
});
但你可能需要这样做
return requestsPromise.then(function() { // again, return the result of the call
var requestData = this.data; // take it from wherever (unsure what `this` is)
// ^^^ local variable
console.log(requestData); // <-- Data is defined here
return requestData; // and can be returned to resolve the promise with
});
returnrequestspromise。然后(function(){//再次返回调用的结果
var requestData=this.data;//从任何地方获取它(不确定'this'是什么)
//^^^局部变量
console.log(requestData);//我确实需要使用第二个作为requestPromise返回。将requestData定义为全局是一个疏忽。您引用的第二个返回对我有用。非常感谢!我会投票,但我还没有代表;)我确实需要使用第二个作为requestPromise返回。将requestData定义为全局是一个疏忽。您引用的第二个返回对我有效。非常感谢!我想投票,但我还没有代表;)