我如何 return 从承诺到我的调用函数的值?
How do I return a value from a promise to my calling function?
我的理解是,我正在调用一个同步函数 "httpGet",它应该返回一个承诺。该函数似乎可以正常工作,因为它通过 console.log 语句成功地从 Airtable 获取了我的所有数据,但我在 .then() 方法中得到了任何响应。
这里是我调用 "httpGet" 函数的地方:
async handle(handlerInput) {
let speechText = '';
console.log('Going to fetch Airtable data');
await httpGet(base).then((response) => {
console.log('have promise')
}).catch((err) => {
//set an optional error message here
console.log('do not have promise')
//speechText = 'there is an error ' + err.message;
});
speechText = `Container ID ` + contID + ` is a ` + bincolor + `, ` + gallons + ` located in ` + binloc ;
console.log('speechText = ' + speechText);
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Warehouse Inventory', speechText)
.getResponse();
};
这是正在调用的 httpGet 函数:
async function httpGet(options) {
// return new pending promise
return new Promise((resolve, reject) => {
base('Bins').select({
// Selecting a record in Grid view:
maxRecords: 1,
view: "Grid view"
}).eachPage(function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
records.forEach(function(record) {
console.log('Container ID: ', record.get('Container ID'));
console.log('Gallons: ', record.get('Gallons'));
console.log('Bin Color: ', record.get('Color'));
console.log('Location: ', record.get('Location'));
console.log('Imperfections: ', record.get('Imperfections'));
var contID = record.get('Container ID');
var gallons = record.get('Gallons');
var bincolor = record.get('Color');
var binloc = record.get('Location');
var imper = record.get('Imperfections');
});
// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
}, function done(err) {
if (err) {
console.error(err); return;
}
});
});
}
最终,我试图获取存储在变量 contID、加仑、bincolor、binloc 和 imper 中的值。我该如何做到这一点?
在 httpGet()
中,执行 resolve({ contID, gallons, /* ... */ })
,然后在 then((response) => {})
中,您的 response
将成为具有 contID
、gallons
的对象,等属性。查看 this blog post 了解有关异步 return 值的更多详细信息。
您的代码有太多嵌套,这是一种不好的做法。你不能 return 从承诺中获取价值,因为它是异步的,你可以在其中做你想做的事。
httpGet = (options) => {
return new Promise((resolve, reject) => {
resolve(/*return your result here*/);
reject(/*return error here*/);
});
}
//inside of some function
let firstResult, secondResult;
httpGet(argument)
.then(result => {
firstResult = result;
//you can chain promise if u need
return httpGet(secondArgument)
})
.then(result => {
secondResult = result;
})
.then(() => {
//here you can acces firstResult, secondResult variables, pass them to some function as arguments
})
.catch((error)=>{})
我的理解是,我正在调用一个同步函数 "httpGet",它应该返回一个承诺。该函数似乎可以正常工作,因为它通过 console.log 语句成功地从 Airtable 获取了我的所有数据,但我在 .then() 方法中得到了任何响应。
这里是我调用 "httpGet" 函数的地方:
async handle(handlerInput) {
let speechText = '';
console.log('Going to fetch Airtable data');
await httpGet(base).then((response) => {
console.log('have promise')
}).catch((err) => {
//set an optional error message here
console.log('do not have promise')
//speechText = 'there is an error ' + err.message;
});
speechText = `Container ID ` + contID + ` is a ` + bincolor + `, ` + gallons + ` located in ` + binloc ;
console.log('speechText = ' + speechText);
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Warehouse Inventory', speechText)
.getResponse();
};
这是正在调用的 httpGet 函数:
async function httpGet(options) {
// return new pending promise
return new Promise((resolve, reject) => {
base('Bins').select({
// Selecting a record in Grid view:
maxRecords: 1,
view: "Grid view"
}).eachPage(function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
records.forEach(function(record) {
console.log('Container ID: ', record.get('Container ID'));
console.log('Gallons: ', record.get('Gallons'));
console.log('Bin Color: ', record.get('Color'));
console.log('Location: ', record.get('Location'));
console.log('Imperfections: ', record.get('Imperfections'));
var contID = record.get('Container ID');
var gallons = record.get('Gallons');
var bincolor = record.get('Color');
var binloc = record.get('Location');
var imper = record.get('Imperfections');
});
// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
}, function done(err) {
if (err) {
console.error(err); return;
}
});
});
}
最终,我试图获取存储在变量 contID、加仑、bincolor、binloc 和 imper 中的值。我该如何做到这一点?
在 httpGet()
中,执行 resolve({ contID, gallons, /* ... */ })
,然后在 then((response) => {})
中,您的 response
将成为具有 contID
、gallons
的对象,等属性。查看 this blog post 了解有关异步 return 值的更多详细信息。
您的代码有太多嵌套,这是一种不好的做法。你不能 return 从承诺中获取价值,因为它是异步的,你可以在其中做你想做的事。
httpGet = (options) => {
return new Promise((resolve, reject) => {
resolve(/*return your result here*/);
reject(/*return error here*/);
});
}
//inside of some function
let firstResult, secondResult;
httpGet(argument)
.then(result => {
firstResult = result;
//you can chain promise if u need
return httpGet(secondArgument)
})
.then(result => {
secondResult = result;
})
.then(() => {
//here you can acces firstResult, secondResult variables, pass them to some function as arguments
})
.catch((error)=>{})