headers: {
                  
                  
                      'Accept': 'application/json',
                  
                  
                      'Content-Type': 'application/json',
                  
                  
                    },
                  
                  
                    body: JSON.stringify({
                  
                  
                      userid: userid
                  
                  
                    })
                  
                  
                  }).then(res => res.json()
                  
                  
                  ).then(function(data) {
                  
                  
                    bal = data.bal;
                  
                  
                  });
                  
                  
                  console.log(bal);
                  
                  
                  В консоли выдает 0 почему значение пеpеменной не меняется?
                  
                  
                
gist.github.com
https://learn.javascript.ru/async https://learn.javascript.ru/event-loop
потому-что console.log тоже надо внутрь асинхронки засунуть...
.
после bal = data.bal; console.log поставьте
var bal = 0; async function getData() { var resp = await fetch('url', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ userid: userid }) }); let data = await resp.json() return data; } getData().then(data => bal = data.bal); console.log(bal); Попpобовал так, всё pавно(
Обсуждают сегодня