Is there a way to wait until a function is finished?

前端 未结 3 589
时光取名叫无心
时光取名叫无心 2021-01-24 23:20

I\'m trying to get information (true/false) from AsyncStorage in a function and create a string which is importent to fetch data in the next step. My problem is, the function is

3条回答
  •  感情败类
    2021-01-25 00:09

    Try this instead. Async functions and Promises can be tricky to get right and can be difficult to debug but you're on the right track.

    async _getFetchData() {
        let channels = "";
    
        let results = await AsyncStorage.getAllKeys();
    
        results.forEach((result) => {
            if (result.includes("not") === false) {
                let item = await AsyncStorage.getItem(result);
    
                if (item === 'true') {
                    console.log(`channel: ${result}`)
    
                    channels = `channel_id ${result}`;
                }
            }
        });
    
        return channels;
    }
    
    _fetchData() {
        this._getFetchData().then((channels) => {
            console.log(`channel required: ${channel}`);
        });
    }
    

提交回复
热议问题