export function funcBSPos(diffMarkPoint, newLineHL, cycle) { console.log(cycle, '---') let newData = [] let pos = 0 if (cycle === '1h') { // let csvdata = [] newData = funcGetCsv().then((data) => { // 父函数调用子函数, 子函数有.then() 父函数必须要.then() return data; }); } return newData; let markBSPos = funcBSPos(diffMarkPoint, newLineHL, this.cycle).then((data) => { // 父函数调用子函数, 子函数有.then() 父函数必须要.then() return data; }); console.log(markBSPos) 父函数调用子函数用了.then() 为什么报错(0 , _T_bspos_bspos_js__WEBPACK_IMPORTED_MODULE_13__.funcBSPos)(...).then is not a function TypeError: (0 , _T_bspos_bspos_js__WEBPACK_IMPORTED_MODULE_13__.funcBSPos)(...).then is not a function at VueComponent.getCharts (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/HelloWorld.vue?vue&type=script&lang=js&:225:123) at eval (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/HelloWorld.vue?vue&type=script&lang=js&:267:14)
时间: 2024-04-13 20:30:17 浏览: 20
这个错误是因为你的函数`funcBSPos`没有返回一个`Promise`对象,所以不能使用`.then()`方法。你可以修改你的代码,将`funcGetCsv()`函数返回一个`Promise`对象,这样就可以链式调用`.then()`方法了。修改后的代码如下:
```javascript
export function funcBSPos(diffMarkPoint, newLineHL, cycle) {
console.log(cycle, '---')
let newData = []
let pos = 0
if (cycle === '1h') {
newData = funcGetCsv().then((data) => {
return data;
});
}
return newData;
}
let markBSPos = funcBSPos(diffMarkPoint, newLineHL, this.cycle).then((data) => {
console.log(data);
});
console.log(markBSPos);
```
请注意,在这个例子中,`funcGetCsv()`函数需要返回一个`Promise`对象才能使用`.then()`方法来处理异步操作的结果。
相关问题
export async function funcBSPos(diffMarkPoint, newLineHL, cycle) { console.log(cycle, '---') let newData = [] let pos = 0 if (cycle === '1h') { newData = funcGetCsv().then((data) => { return data; }); } return newData; } let makrPos = funcBSPos(diffMarkPoint, newLineHL, this.cycle).await ; 那这样也是可以的?
抱歉,你的代码中有一些语法错误。在`await`关键字后面应该跟着一个`Promise`对象,而不是直接使用`.await`。另外,使用`await`关键字的函数必须是`async`函数。以下是修正后的代码示例:
```javascript
export async function funcBSPos(diffMarkPoint, newLineHL, cycle) {
console.log(cycle, '---')
let newData = []
let pos = 0
if (cycle === '1h') {
newData = await funcGetCsv();
}
return newData;
}
async function processData() {
let markPos = await funcBSPos(diffMarkPoint, newLineHL, this.cycle);
console.log(markPos);
}
processData();
```
在调用`funcBSPos()`函数时,使用`await`关键字等待异步操作完成,并将结果赋值给`markPos`变量。然后,在`processData()`函数中使用`await`关键字等待异步操作完成后打印结果。
希望这次的回答能够帮到你,如果还有其他问题,请随时提问。
export async function funcBSPos(diffMarkPoint, newLineHL, cycle) 这个是对的,那为什么 funcBSPos(diffMarkPoint, newLineHL, this.cycle).then((data) => { console.log(data); }); 这个.then()里面不用return data?
抱歉,我的回答有误。在`async`函数内部使用`return`关键字会将返回值包装在一个`Promise`对象中,因此在`.then()`方法中不需要再使用`return`。
以下是修改后的代码示例:
```javascript
export async function funcBSPos(diffMarkPoint, newLineHL, cycle) {
console.log(cycle, '---')
let newData = []
let pos = 0
if (cycle === '1h') {
newData = await funcGetCsv(); // 使用await等待异步操作完成
}
return newData;
}
funcBSPos(diffMarkPoint, newLineHL, this.cycle).then((data) => {
console.log(data);
});
```
在`funcBSPos()`函数中使用`await`关键字等待异步操作完成后,直接返回`newData`即可。在调用`.then()`方法时,将异步操作的结果作为参数传递给回调函数,并在回调函数中处理结果。不需要再使用`return`关键字。
非常抱歉之前的回答有误导,希望这次的回答能够解决你的疑问。如果还有其他问题,请随时提问。