javascript - 如何使用导入的函数作为承诺。Object(...)(...).then 不是函数
问题描述
我有以下问题。我在一个单独的文件中创建了这个函数
import Cookies from 'js-cookie';
import jwtDecode from 'jwt-decode';
export default function checkLifeExpectancySet(){
var decoded = [];
decoded.permited = [];
var accesstoken = Cookies.get('accesstoken');
if((accesstoken)){
var decoded = jwtDecode(accesstoken)
}
//if it includes the life_expectancy it means it hasnt been set
if(decoded.permited.includes("life_expectancy")){
return false;
}
}
然后我这样称呼它
checkLifeExpectancySet().then((result) =>{
if(result == true){
//the lifeExpectancy is yet to set
this.props.lifeExpectancySet(false)
}else{
//the lifeExpectancy was already set
this.props.lifeExpectancySet(true)
}
})
但它返回
TypeError: Object(...)(...).then 不是函数
我认为这是一个问题,因为它不是承诺或其他东西,我将如何将其转换为承诺或等待结果?我现在迷路了
解决方案
jwtDecode 没有返回承诺,因此,如果您想返回承诺,请执行此操作。
export default function checkLifeExpectancySet(){
return new Promise((resolve, reject) => {
let decoded;
const accesstoken = Cookies.get('accesstoken');
if(accesstoken){
decoded = jwtDecode(accesstoken)
}
// your other logic.
// if you want to throw error, uncomment below line.
// return reject(new Error('some reason'));
return resolve(decoded);
});
}
推荐阅读
- java - Parent POM vs Super POM - 他们是一样的吗?
- node.js - 使用 mongoDB 在 mongoose 中获取空数组
- reactjs - 使用 React 从 WebAPI JSON 数据创建列表?地图不是函数错误
- c# - 从 C# 调用时无法识别 mysqldump 命令
- playwright - 相当于 cy.scrollTo('bottom') 的剧作家
- ios - 如何将 UICollectionViewDiffableDataSource reorderHandlers 与自定义组合布局一起使用?
- mips - 我不知道如何在 MIPS 编程中处理数组
- arrays - 如何使用powershell将字符串变量拆分为两个数组对象变量?
- python - 向 GCP IoT Core 发送状态和事件后,paho mqtt python 客户端经常断开连接并出现错误代码 7“连接丢失”
- c# - 如何匹配另一个对象的旋转增量?