Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置承诺对变量的响应_Javascript_Typescript_Promise_Async Await - Fatal编程技术网

Javascript 如何设置承诺对变量的响应

Javascript 如何设置承诺对变量的响应,javascript,typescript,promise,async-await,Javascript,Typescript,Promise,Async Await,我有一个返回值的异步函数 async getUsername(env:string,skuName: string,token:string): Promise<any> { let value = await this.getDeviceName(env,skuName,token); return value; }; 变量productN正在工作,因为我可以在日志中看到响应,但是当我尝试在这个块之外使用productN时,我遇到了未定义的问题

我有一个返回值的异步函数

  async getUsername(env:string,skuName: string,token:string): Promise<any> {
      let value =  await this.getDeviceName(env,skuName,token);
      return value;
  };
变量productN正在工作,因为我可以在日志中看到响应,但是当我尝试在这个块之外使用productN时,我遇到了未定义的问题

 promotion = { 
    name: productN,
    startDate: start,
    endDate: end
 };
我正在尝试将name设置为productN,但我就是无法让它工作


有人能解释一下我做错了什么吗?谢谢

当您收到回复时,您可以将以下内容分配给
促销

const prod = this.getUsername(env,skuName,token).then((resp) => {
    productN =  resp;
    promotion.name = resp
    this.wait(300);
});
由于您的
getUsername
是异步的,因此可以使用
wait
等待响应,然后分配给
promotion
对象

(async() => {
  const resp = await this.getUsername(env,skuName,token);
  promotion = {
    name: resp,
    startDate: start,
    endDate: end
  } 
})();
---编辑---

const输入=[
{
环境:“洛伦”,
skuname:'skuname1',
标记:“哑巴”
}, 
{
环境:“洛伦”,
skuname:'skuname2',
标记:“哑巴”
}
];
const getUserName=(username)=>{
返回新承诺(解决=>{
setTimeout(()=>解析(用户名),2000);
});
};
(异步()=>{
const resp=wait Promise.all(input.map({skuname})=>getUserName(skuname));
控制台日志(resp);
})();
//或
Promise.all(input.map(({skuname})=>getUserName(skuname)))
。然后(resp=>{
控制台日志(resp);
});试试看


你能试试吗?返回
getUsername
中的
getDeviceName
函数

getUsername(env:string, skuName: string, token:string): Promise<any> {
  return this.getDeviceName(env, skuName, token);
};
const productN = await this.getUsername(env, skuName, token);
console.log(productN);
如果您想在
getUsername
中执行任何其他操作。你可以让它回报你的承诺

getUsername(env:string, skuName: string, token:string): Promise<any> {
 return New Promise(async (resolve,reject)=>{
  try {
   let value =  await this.getDeviceName(env,skuName,token);
   ...
   //all other things you want to do here 
   ...
   resolve(value);
  } catch (error) {
   reject(error)
  }
 }
};

const productN = await this.getUsername(env, skuName, token);
console.log(productN);
getUsername(env:string,skuName:string,token:string):承诺{
返回新承诺(异步(解析、拒绝)=>{
试一试{
让value=wait this.getDeviceName(env、skuName、token);
...
//你想在这里做的所有其他事情
...
决心(价值);
}捕获(错误){
拒绝(错误)
}
}
};
const productN=wait this.getUsername(env、skuName、token);
console.log(productN);

注意:我使用了
try catch
块进行错误处理,如果您不想忽略它。

您可以在收到响应时为
promotion.name
赋值。比如
promotion.name=resp
。因为productN在您的承诺解决之前运行。这就是javascript的工作方式。嘿@randomSoul,感谢您的快速响应se,我尝试了你的两个答案……当我使用第一个答案时,表中只有最后一行选择了升级。name,前10行未定义。当我尝试第二个答案时,表中的所有数据都为空。升级是一个对象。因此,每次分配新值时,前一个值都会被覆盖。似乎存在多个值ple记录,您可以创建一个承诺数组,然后使用
promise.all
。因此,一旦所有承诺都得到执行,您就可以在表中显示它们。
getUsername(env:string, skuName: string, token:string): Promise<any> {
  return this.getDeviceName(env, skuName, token);
};
const productN = await this.getUsername(env, skuName, token);
console.log(productN);
const productN = await this.getDeviceName(env, skuName, token);
getUsername(env:string, skuName: string, token:string): Promise<any> {
 return New Promise(async (resolve,reject)=>{
  try {
   let value =  await this.getDeviceName(env,skuName,token);
   ...
   //all other things you want to do here 
   ...
   resolve(value);
  } catch (error) {
   reject(error)
  }
 }
};

const productN = await this.getUsername(env, skuName, token);
console.log(productN);