我如何在回调函数中设置变量?

问题描述 投票:0回答:1

这是一个纯JavaScript文件。如何将我的客户变量设置为通话中返回的数据?

function test(columnName) {
  var customers = [];
  $pnp.sp.web.lists.getByTitle("Documents").fields.getByTitle(columnName).get().then(f => {
    if (f.Choices) {
      customers = f.Choices;
      console.log(f.Choices);
    }
  });
javascript variables callback
1个回答
0
投票

使用承诺。

function test(columnName) {
    return new Promise((resolve, reject) => {
        $pnp.sp.web.lists.getByTitle("Documents").fields.getByTitle(columnName).get().then(f => {
            if (f.Choices) {
                resolve(f.Choices);
            }
            else {
                reject("Nothing found.");
            }
        });
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.