javascript - 正确链接 Promise
问题描述
如果它不存在,我想创建一个列表,然后创建字段,也就是确保。我无法理解如何设置承诺:
public getLocations(): Promise<ILocations[]> {
let promise: Promise<ILocations[]> = new Promise<ILocations[]>((resolve, reject): void => {
sp.web.lists.ensure("DeliveryBoardLocations")
.then((ler: ListEnsureResult): Promise<ListEnsureResult> => {
console.log("got list ensure result");
if (ler.created) {
console.log("Locations was created");
let batch = sp.web.createBatch();
ler.list.fields.inBatch(batch).addText("textField");
ler.list.fields.inBatch(batch).addDateTime("dateField");
ler.list.fields.inBatch(batch).addBoolean("booleanField");
batch.execute()
.then((): Promise<ListEnsureResult> => {
console.log("fields created");
let batch = sp.web.createBatch();
let view = ler.list.defaultView;
view.fields.inBatch(batch).add("textField");
view.fields.inBatch(batch).add("dateField");
view.fields.inBatch(batch).add("booleanField");
batch.execute()
.then((): Promise<ListEnsureResult> => {
console.log("fields added to view; returning ler");
return ler;
});
});
// return;
}
else {
console.log("Locations already existed; returning ler");
resolve(ler.list);
return Promise.reject("List Exists");
}
console.log("after if stmt");
})
.then((ler): void => {
console.log("ler: ", ler);
console.log("ler.list: ", ler.list);
console.log("ler.list.items: ", ler.list.items);
resolve(ler.list.items.getAll());
});
});
return promise;
}
控制台显示:
LocationsService.ts:16 got list ensure result
LocationsService.ts:19 Locations was created
LocationsService.ts:56 after if stmt
LocationsService.ts:59 ler: undefined
LocationsService.ts:31 fields created
LocationsService.ts:43 fields added to view; returning ler
我想打电话确保;如果结果返回列表已创建,则创建列并显示它们。然后返回列表,以便我可以获取所有项目并返回项目。如果创建了列表,则返回项目。
我很难弄清楚应该如何处理。
解决方案
代码的根本缺陷是缺少返回,在承诺链的情况下,返回传递数据和控制流。
您也可以清除new Promise()
包装纸。没有包装纸的链条启动sp.web.lists.ensure()
是完全足够的。
您还需要处理类型转换子句。Promise<ListEnsureResult>
可能并非在所有情况下都是正确的。试着<any>
让自己开始。
public getLocations(): Promise<ILocations[]> {
// let promise: Promise<ILocations[]> = new Promise<ILocations[]>((resolve, reject): void => { // not necessary
return sp.web.lists.ensure('DeliveryBoardLocations')
^^^^^^
.then((ler: ListEnsureResult): Promise<ListEnsureResult> => { // <any> ?
if (ler.created) {
let batch = sp.web.createBatch();
ler.list.fields.inBatch(batch).addText('textField');
ler.list.fields.inBatch(batch).addDateTime('dateField');
ler.list.fields.inBatch(batch).addBoolean('booleanField');
return batch.execute()
^^^^^^
.then((): Promise<ListEnsureResult> => { // <any> ?
let batch = sp.web.createBatch();
let view = ler.list.defaultView;
view.fields.inBatch(batch).add('textField');
view.fields.inBatch(batch).add('dateField');
view.fields.inBatch(batch).add('booleanField');
return batch.execute()
^^^^^^
.then((): Promise<ListEnsureResult> => ler); // <any> ?
});
}
else {
// resolve(ler.list); // not necessary
// return Promise.reject('List Exists'); // eek, confounding after resolve.
throw new Error('List Exists'); // it's more economical to throw rather than return Promise.reject(...). Also, be sure to throw an Error() object.
}
})
.then((ler): void => {
// resolve(ler.list.items.getAll()); // not necessary
return ler.list.items.getAll();
});
// });
// return promise;
}
在实践中,您可能会选择通过重新排列(和清理)来稍微简化:
public getLocations(): Promise<ILocations[]> {
return sp.web.lists.ensure('DeliveryBoardLocations')
.then((ler: ListEnsureResult): Promise<any> => {
if (ler.created) {
let batch = sp.web.createBatch();
ler.list.fields.inBatch(batch).addText('textField');
ler.list.fields.inBatch(batch).addDateTime('dateField');
ler.list.fields.inBatch(batch).addBoolean('booleanField');
return batch.execute()
.then((): Promise<any> => {
let batch = sp.web.createBatch();
let view = ler.list.defaultView;
view.fields.inBatch(batch).add('textField');
view.fields.inBatch(batch).add('dateField');
view.fields.inBatch(batch).add('booleanField');
return batch.execute();
})
.then(() => ler.list.items.getAll()); // rearranged
} else {
throw new Error('List Exists');
}
});
}
推荐阅读
- python - 将方法放在views.py中的类中
- python - 使用 Python 将 OpenCV cv.Rectangle(img, pt1, pt2) 转换为 NumPy 数组
- css - 创建具有视差效果的图像的 css 网格
- macos - 如何在 mac os 终端中安装 hcitool
- python - 在 Jenkins 中执行 Pyinstaller
- php - 更新外键列的查询问题
- r - 如何在数据框中的特定列中找到模式?
- python-3.x - Python - 找到两个复杂坐标的平均值
- python - 以相反的顺序从numpy数组中获取一些元素
- excel - 如何将应用过滤器应用于工作表的 3 个不同列