Visualizzazione dei risultati da 1 a 5 su 5

Discussione: funzione asincrona

  1. #1

    funzione asincrona

    Ciao, su questa funzione:

    codice:
    async function getStartChat() {
            const response = await fetch("lib/bot.php").
            then(response => response.text()).
            then(data => { return data }).
            catch(err => console.log(err));
        }

    mi restituisce questo:
    [object Promise]

    perchè?

  2. #2
    ho modificato anche in questo modo:

    codice:
    async function getStartChat() {
        const response = await fetch("lib/start_chat.php")
        let data = await response.text();	
        return data;
    }
    
    
    let saveChat = getStartChat();
    if(saveChat !== "") qCHAT.innerHTML = saveChat;
    ma sempre stesso risultato

  3. #3
    Utente di HTML.it L'avatar di ninja72
    Registrato dal
    May 2020
    residenza
    -
    Messaggi
    319
    Le promises le puoi gestire in diversi modi ma sempre in modo asyncrono.

    codice:
    // async await method
    const getStartChat = async () => (await fetch("test.txt")).text()
    
    getStartChat()
        .then(data => {
            if (data !== "") qCHAT.innerHTML = data;
        })
    
    //--------------------------------------------------------------
    
    // chaining promises method
    const promiseReturn = fetch("test.txt")
        .then(response => response.text())
        .then(data => data)
    
    const getStartChat = async () => {
        const data = await promiseReturn
        if (data !== "") qCHAT.innerHTML = data;
    }
    getStartChat()

  4. #4
    Si grazie avevo capito dove ho sbagliato ed è identico a quello da te istruito, grazie ancora, solo una domanda, perche "promiseReturn" lo metti fuori dalla funzione e non interno?
    Perchè non posso metterci un return alla funzione?
    Ultima modifica di techno; 22-04-2022 a 16:31

  5. #5
    Utente di HTML.it L'avatar di ninja72
    Registrato dal
    May 2020
    residenza
    -
    Messaggi
    319
    Detta in breve, per separare quelli che sono i dati restituiti dalla promises da quelli di presentazione della view.
    Infatti potrei avere l'esigenza di catturare lo stesso dato ed applicarlo su views differenti, tutto dipende poi da cosa devi fare.

    Errata corrige:
    Nel secondo esempio questa linea di codice è superflua perchè in realtà non viene utilizzata per un eventuale view del dato all'interno della promises stessa.
    codice:
    .then(data => data)
    In questo esempio wrappo tutto in una funzione asincrona, questo per gestire più facilmente le view in modo asincrono con await.

    codice:
            const qCHAT = document.getElementById('chat')
            const qCHAT2 = document.getElementById('chat2');
    
            window.addEventListener('DOMContentLoaded', async () => {
                // async await method
                const getStartChat = async () => (await fetch("file.txt")).text();
                // chaining promises method
                const getStartChat2 = () => {
                    const promiseReturn = fetch("file.txt")
                        .then(response => response.text())
                    return promiseReturn
                }
                qCHAT.textContent = await getStartChat()
                qCHAT2.textContent = await getStartChat2()
            });

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.