正确链接承诺

时间:2018-09-13 21:09:14

标签: javascript typescript sharepoint 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

我想打电话给确保;如果返回结果是创建列表,则创建列并显示它们。然后返回列表,以便我可以获取所有项目并返回项目。如果创建了列表,则返回项目。

我很难弄清楚应该如何处理。

2 个答案:

答案 0 :(得分:1)

代码的本质失败之处在于缺少回报,在承诺链的情况下,回报传达了数据控制流。

您还可以清除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');
        }
    });
}

答案 1 :(得分:0)

您实际上需要返回batch承诺,否则外包装承诺将立即完成执行,而不是等待它们并给出结果。

return 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");

        return batch.execute()
            .then((): Promise<ListEnsureResult> => {
                console.log("fields added to view; returning ler");

                return ler;
            });
    });