Git Product home page Git Product logo

Comments (14)

liam61 avatar liam61 commented on June 12, 2024 1
function getData(urls) {
	return new Promise((resolve, reject) => {
		const res = Array.from({ length: urls.length });

		urls.forEach((url, i) =>
			fetch(url).then(data => {
				res[i] = [i, data];
				check();
			}, reject)
		);

		function check(i) {
			const realRes = res.filter(Boolean);
			for (let k = 0; k < realRes.length; k++) {
				if (realRes.map(([j]) => j)[k] !== k) return;
			}
			resolve(realRes.map(([_, data]) => data));
		}
	});
}

你这个 realRes 没把数组锁住,先返回的数据会白 resolve 掉,再者 check 算法遍历数组太多次,数据多了会造成性能问题哦

607F7986-F230-48A6-B21C-A628793801EA

from blog.

jinxin0112 avatar jinxin0112 commented on June 12, 2024

这题不能用 promise 吗

from blog.

liam61 avatar liam61 commented on June 12, 2024

这题不能用 promise 吗

不清楚,欢迎指教哦

from blog.

jinxin0112 avatar jinxin0112 commented on June 12, 2024

Promise.all 包4个fetch ?

from blog.

liam61 avatar liam61 commented on June 12, 2024

Promise.all 包4个fetch ?

读题哦,是顺序发送,promise.all 是并发。然后是一个请求如果拿到结果,并且顺序为1,就马上输出,如果顺序为2,等顺序为1的请求输出后再输出,promise.all 是等全部请求都成功了才返回结果

from blog.

jinxin0112 avatar jinxin0112 commented on June 12, 2024
function getData(urls) {
	return new Promise((resolve, reject) => {
		const res = Array.from({ length: urls.length });

		urls.forEach((url, i) =>
			fetch(url).then(data => {
				res[i] = [i, data];
				check();
			}, reject)
		);

		function check(i) {
			const realRes = res.filter(Boolean);
			for (let k = 0; k < realRes.length; k++) {
				if (realRes.map(([j]) => j)[k] !== k) return;
			}
			resolve(realRes.map(([_, data]) => data));
		}
	});
}

from blog.

jinxin0112 avatar jinxin0112 commented on June 12, 2024

@lawler61 你这个直接return res怕是不得行哦

from blog.

liam61 avatar liam61 commented on June 12, 2024

@lawler61 你这个直接return res怕是不得行哦

你说的这个是对的哈,整体包个 promise 即可

from blog.

jinxin0112 avatar jinxin0112 commented on June 12, 2024

受教了。。😄

from blog.

vnues avatar vnues commented on June 12, 2024

async await不行吗

from blog.

liam61 avatar liam61 commented on June 12, 2024

async await不行吗

可以给个demo,不过请理解题目,是顺序(并行)发送,不是一个请求发送且响应后再发下一个,那这就不是面试题了

from blog.

gaoxinxiao avatar gaoxinxiao commented on June 12, 2024

试了一下用Promise.all好像确实可以

let url = 'http://localhost:8080'

    async function p1(param) {
        return await fetch(url + param).then(res => res.json()).then(res => res)
    }
    async function p2(param) {
        return await fetch(url + param).then(res => res.json()).then(res => res)
    }

    async function p3(param) {
        return await fetch(url + param).then(res => res.json()).then(res => res)
    }

    async function p4(param) {
        return await fetch(url + param).then(res => res.json()).then(res => res)
    }

    Promise.all([p1('/data.json'), p2('/data2.json'), p3('/data3.json'), p3('/data4.json')]).then(res => {
        console.log(res, '结果') //[1,2,3,4]
    })

const express = require('express');
const cors = require('cors');
const app = express();

const getTime = () => Math.floor(Math.random() * 4 * 1000);

app.use(cors({ origin: '*' }));

app.use('/data.json', (req, res) => {
setTimeout(() => res.end(JSON.stringify({ a: 1 })), 5000);
});

app.use('/data2.json', (req, res) => {
setTimeout(() => res.end(JSON.stringify({ b: 2 })), 1000);
});

app.use('/data3.json', (req, res) => {
setTimeout(() => res.end(JSON.stringify({ c: 3 })), 600);
});

app.use('/data4.json', (req, res) => {
setTimeout(() => res.end(JSON.stringify({ d: 4 })), 100);
});

app.listen(8080, () => console.log('the app is running at http://localhost:8080'));

from blog.

liam61 avatar liam61 commented on June 12, 2024

@gaoxinxiao 是a一回来就输入哦,如果是 b 先回来,要等a输出了,才接着输出,而不是Promise.all 所有接口完成后才看到接口。即使是Promise.all 原理能不能手写出来呢~

from blog.

gaoxinxiao avatar gaoxinxiao commented on June 12, 2024

@gaoxinxiao是一回来就输入哦,如果是b先回来,要等一个输出,才输出,而不是承诺。所有接口所有完成后看到接口。即使是承诺。所有原理能不能手写出来呢~

我试了一下你写的那个例子也是一起回来的 就算先请求a然后a慢返回b先返回 也是一起展示的

from blog.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.