, поэтому я написал асинхронную функцию, как это : < /p>
Код: Выделить всё
async loadMap () {
try{
let response = await fetch(this.filePath)
return await response.text();
}catch(e) {
throw new Error(e)
}
}
< /code>
И после этого я собираюсь взять его и написать код, чтобы сделать матрицу n*y, чтобы отобразить ее в браузере.
Это весь код класса карты у этого есть функция loadmap () Код: Выделить всё
class Map {
constructor(filePath , col , row) {
this.filePath = filePath
this.map = null ;
this._initMap(col , row)
}
async _initMap(col , row) {
this.map = await this._getMap(col , row);
console.log("map is inited" , this.map)
}
async loadMap () {
try{
let response = await fetch(this.filePath)
return await response.text();
}catch(e) {
throw new Error(e)
}
}
async _getMap(mapCol , mapRow) {
let array = [...new Array(mapRow)].map(() => new Array(mapCol).fill('#'))
let stringMap = await this.loadMap();
let arrayMap = stringMap.split('').filter(s => !(s == ' ') && !(s == "\r"))
let col = 0 ;
let row = 0 ;
let index = 0 ;
while(row < array.length) {
array[row][col] = arrayMap[index]
col++ ; index ++ ;
if(arrayMap[index] == '\n') {
arrayMap.splice(index , 1);
col = 0 ;row++ ;
}
}
return array
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... asyncawait
Мобильная версия