mmlpxjs / mmlpx

🐘 mobx model layer paradigm
MIT License
180 stars 10 forks source link

useStrict与async action的一个问题 #4

Closed xiaodongzai closed 6 years ago

xiaodongzai commented 6 years ago

遵循CQRS规范,action中如果有返回值则抛出异常。async会返回Promise,就会报错,这种情况怎么处理比较好? useStrict(true);

      @action.bound
    replaceAll(todos: Todo[]) {
        this.list = todos;
    }

    // async action
    @action
    async loadTodos() {
                this.isPending = true;
        const r = await api.getTodos();
        this.replaceAll(r.data.d);
               // ...
    }
xiaodongzai commented 6 years ago
if (returnedValue && !isPromise(returnedValue)) {
    throw new SyntaxError('you should not return any values from actions when you enable the strict mode!');
}

我尝试做了修改,想把async action的情况排除外,但感觉不太合理。此外加了上述代码发现,在做单测时,发现async action又返回的是undefined。

@action.bound
async asyncAction() {
    await new Promise((resolve) => {
        setTimeout(() => {
            this.update();
            resolve();
        }, 1000);
    });
}
expect(() => vm.store.asyncAction()).toThrow(SyntaxError); // false
expect(isPromise(vm.store.asyncAction())).toBe(true); // false
expect(vm.store.asyncAction()).toBeUndefined(); // true
kuitos commented 6 years ago

@xiaodongzai OK,promise 这种问题这两天我找个时间处理一下。 Thanks~

kuitos commented 6 years ago

修复为:返回的 promise 会被忽略,且如果 promise 最后 resolve 的值为 undefined 则不会 throw exception,否则依然报错。 Thanks!

kuitos commented 6 years ago

released v3.4.3