第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Fetch() 亂序運(yùn)行

Fetch() 亂序運(yùn)行

夢(mèng)里花落0921 2023-05-25 16:56:18
我正在嘗試讓 API 提取一個(gè)詞并將其設(shè)置為狀態(tài)。然后一個(gè)函數(shù)將讀取該狀態(tài)并完成其設(shè)計(jì)目的。然而,我最初編碼它的方式稱它為亂序。第三個(gè)代碼片段允許代碼成功運(yùn)行,但我不確定為什么。有人能解釋一下有什么區(qū)別或者為什么原來(lái)的方法不起作用嗎?下面是第二個(gè)函數(shù)后面的 API 函數(shù)。wordNikApi = () => {        fetch("http://api.wordnik.com:80/v4/words.json/randomWords?hasDictionaryDef=true&minCorpusCount=0&minLength=5&maxLength=15&limit=1&api_key=/* Removed */")            .then( res => res.json() )            .then( ( result ) => {                this.setState({                    apiWord: result[0].word,                });                console.log("wordNikApi: ", this.state.apiWord);            })            .catch( ( error ) => {                console.log("API ERROR: ", error);            })    };resetGame = () => {        console.log("resetGame");        this.wordNikApi();        this.setState({             word: [],            count: 0,            allAttempts: [],            letterIndex: [],            numberOfBadAttempts: 0,            remainingAttempts: 6,            repeat: false,            pageLock: false,            invalidKey: false,        }, () => {            console.log("resetGame: function 1");            console.log(this.state.apiWord);            let fullWord = "word";            let wordArray = fullWord.split("");            let wordLength = wordArray.length;            // Send wordObj to state with value and index            let wordObj = wordArray.map((value, index) => {                return {                    found: false,                    val: value,                    id: index,                }            })            this.setState({                 word: wordObj,                wordLength: wordLength,                remainingAttempts: 6,            });        });    };
查看完整描述

3 回答

?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊

導(dǎo)致你出現(xiàn)問(wèn)題的是JS異步,當(dāng)你在resetGame函數(shù)中調(diào)用wordNikApi函數(shù)時(shí),你必須使用await關(guān)鍵字,這樣才能先生成對(duì)wordNijApi函數(shù)的更改,然后繼續(xù)流程工作。嘗試像這樣修改 resetGame 函數(shù):


const resetGame = async()=>{

...


await this.wordNikApi()

...

}


查看完整回答
反對(duì) 回復(fù) 2023-05-25
?
蠱毒傳說(shuō)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

Fetch 是一個(gè)異步函數(shù),這意味著它將與您的其他代碼一起運(yùn)行,調(diào)用設(shè)置this.wordNikApi()獲取請(qǐng)求,但不會(huì)阻止您繼續(xù)編寫(xiě)腳本。

在您的新版本中,函數(shù)內(nèi)部有代碼.then(),當(dāng)提取請(qǐng)求調(diào)用數(shù)據(jù)并返回時(shí)調(diào)用該函數(shù),因此您的代碼在此處等待完成,this.wordNikApi()然后在第三個(gè)代碼段中運(yùn)行。

希望這有助于更清楚地了解 Async 和 Sync,但是有更好的文檔可以解釋這一點(diǎn)。


查看完整回答
反對(duì) 回復(fù) 2023-05-25
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

我實(shí)現(xiàn)了一個(gè)隊(duì)列,它在隊(duì)列中的第一個(gè)項(xiàng)目上調(diào)用 fetch,然后使用 fetch().then 來(lái)拉下一個(gè)項(xiàng)目,發(fā)布它,然后如果隊(duì)列不為空則重復(fù)執(zhí)行。這是我使用的代碼:


var clientDataQueue = [];

function queueClientData(theData) {

? ? clientDataQueue.push(theData);

? ? console.log("++clientDataQueue.length:", clientDataQueue.length)

? ? if (clientDataQueue.length == 1){

? ? ? ? postFromQueue();

? ? }

}


function postFromQueue() {

? ? console.log("--clientDataQueue.length:", clientDataQueue.length)

? ? if (clientDataQueue.length > 0) {

? ? ? ? postClientdata(clientDataQueue[0]).then( () => {

? ? ? ? ? ? clientDataQueue.shift();

? ? ? ? ? ? postFromQueue();

? ? ? ? });

? ? }

}


function postClientdata(theData) {

? ? var htmlData = {

? ? ? ? method: 'POST',

? ? ? ? headers: {

? ? ? ? ? ? 'Content-Type': 'application/json'

? ? ? ? },

? ? ? ? body: JSON.stringify(theData)

? ? };

? ? return fetch('/api/clientData', htmlData)

}


查看完整回答
反對(duì) 回復(fù) 2023-05-25
  • 3 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)