简体   繁体   English

如何从异步 function 返回闭包?

[英]How to return a closure from an async function?

I'm trying to return a function (closure) from a async function.我正在尝试从异步 function 返回 function(闭包)。 So far, the way I have things space is always a promise and not a function, even though it should be a closure.到目前为止,我拥有事物space的方式始终是 promise 而不是 function,即使它应该是一个闭包。

// ...
    const [space, set_space] = React.useState(false);
    React.useEffect(
    () => {
        API.SERVER(query, PAGE_SIZE)
        .then(response => set_space(response));
    },
    [query]
    );

    React.useEffect(
    () => {
        console.log(space.constructor);
        if (space) {
        space(page)
            .then(response => set_titles(Array.from(response.data)));
        }
    },
    [space, page]
    );
// ...

API API

// ...
export default {
    SERVER:async function(search, width)
    {
        if (!search) {
        // return () => {};
        search = DEFAULT_QUERY;
    }
    const all = (await axios.get(SEARCH_URL + `?search=${search}`)).data;
    console.log(all);
    const pages = chunk(all, width);
    // function pager(results) {
    //     const dex = (results > width ? results / width : 0);
    //     const next = pages[dex];
    //     return axios.get(SEARCH_URL + `?fromids=${next}`);
    // }
    // return pager;
    return function(results) {
        const dex = (results > width ? results / width : 0);
        const next = pages[dex];
        return axios.get(SEARCH_URL + `?fromids=${next}`);
    };
    // return pager;
}
};

space.constructor says it's a Promise, but it should be a function that returns axios' Promise, not a Promise it's self. space.constructor says it's a Promise, but it should be a function that returns axios' Promise, not a Promise it's self. I can't really change the way the API is set up because I need to hold the index client side, then walk though it by changing space's parameter.我无法真正改变 API 的设置方式,因为我需要保持索引客户端,然后通过更改空间参数来遍历它。

The problem is, that set_space doesn't handle functions the same way as other values.问题是, set_space处理函数的方式与其他值不同。

That's because React's state setters can take a callback that receives the previous state as an argument.这是因为 React 的 state 设置器可以接受一个回调,该回调接收以前的 state 作为参数。

However, React has no way of differentiating between a callback and a value that happens to be a function.但是,React 无法区分回调和恰好是 function 的值。 So, React will assume it's a callback and calls it, then set the state to contain its return value, another Promise.因此,React 将假定它是一个回调并调用它,然后将 state 设置为包含其返回值,即另一个 Promise。

To put the function itself into the state, you can create a callback that returns the function, like this:要将 function 本身放入 state,您可以创建一个返回 function 的回调,如下所示:

API.SERVER(query, PAGE_SIZE)
.then(response => set_space(() => response));

Hmmm... you just want a function that on call returns you the promise?嗯...您只想要一个 function 随叫随到的返回 promise? What about useCallback then?那么useCallback呢?

const triggerAsync = React.useCallback(
    () => {
        return API.SERVER(query, PAGE_SIZE)
        .then(response => set_space(response))
    },
    [query]
    );

triggerAsync should then return a promise when you execute it然后triggerAsync应该在你执行它时返回一个 promise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM