gpt4 book ai didi

javascript - 调用包含 promise 链的函数

转载 作者:行者123 更新时间:2023-12-03 09:13:18 25 4
gpt4 key购买 nike

我有一段代码调用JS函数(NodeJS)。它调用的函数包含一个Promise链。这是调用该函数的代码:

'use strict'

const request = require('request')

try {
const data = search('javascript')
console.log('query complete')
console.log(data)
} catch(err) {
console.log(err)
} finally {
console.log('all done')
}

function search(query) {
searchByString(query).then( data => {
console.log('query complete')
//console.log(JSON.stringify(data, null, 2))
return data
}).catch( err => {
console.log('ERROR')
console.log(err)
throw new Error(err.message)
})
}

function searchByString(query) {
return new Promise( (resolve, reject) => {
const url = `https://www.googleapis.com/books/v1/volumes?maxResults=40&fields=items(id,volumeInfo(title))&q=${query}`
request.get(url, (err, res, body) => {
if (err) {
reject(Error('failed to make API call'))
}
const data = JSON.parse(body)
resolve(data)
})
})
}

当我运行代码时,控制台将显示 query complete,然后显示搜索结果。

然后我得到一个错误: TypeError: google.searchByString(...).then(...).error is not a function这没有任何意义!为什么会触发此错误?

最佳答案

好吧,您正在将返回值分配给数据,但没有返回 promise 的结果!

这是为您提供的可行解决方案,我唯一要做的就是a)在搜索中返回 promise ,b)从已解决的 promise 中获取数据。

您可以在以下位置查看此代码并进行操作:https://runkit.com/arthur/5827b17b8795960014335852

'use strict'

const request = require('request')

try {
search('javascript')
.then(
data => console.log('and the data', data),
error => console.error('uh - oh', error)
);
console.log('the query isn\'t done yet!');
} catch(err) {
console.error(err);
} finally {
console.log('all done, or is it?')
}

function search(query) {
return searchByString(query).then( data => {
console.log('query complete')
//console.log(JSON.stringify(data, null, 2))
return data
}).catch( err => {
console.log('ERROR')
console.log(err)
throw new Error(err.message)
})
}

function searchByString(query) {
return new Promise( (resolve, reject) => {
const url = `https://www.googleapis.com/books/v1/volumes?maxResults=40&fields=items(id,volumeInfo(title))&q=${query}`
request.get(url, (err, res, body) => {
if (err) {
reject(Error('failed to make API call'))
}
const data = JSON.parse(body)
resolve(data)
})
})
}

以下是我从头开始的方法。我认为我在这里最大的改进是使用了节点url lib。这样做总是一件好事,这样可以为您处理字符串转义。在这种情况下,如果用户要输入类似“do n't”的字符串,则可能会中断:)。
require('request');
// request promise is a popular, well tested lib wrapping request in a promise
const request = require('request-promise');
// i like to use url handling libs, they do good things like escape string input.
const url = require('url');

class Search {
constructor(query) {
this.query = query;
}

fetch() {
const endpoint = url.parse('https://www.googleapis.com/books/v1/volumes');

const options = {
maxResults: 40,
fields: 'items(id,volumeInfo(title))',
q: this.query
}

endpoint.query = options;

const callUrl = url.format(endpoint);
return request.get(callUrl).then(result => JSON.parse(result));
}
}

const search = new Search('javascript');
search.fetch()
.then(result => console.log(result))
.catch(e => console.error('catch:', e));

这是工作代码: https://runkit.com/arthur/5827d5428b24ed0014dcc537

关于javascript - 调用包含 promise 链的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40567515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com