老师。报错 Cannot read property 'then' of undefined

老师。报错 Cannot read property 'then' of undefined

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Document</title>

</head>

<body>

<script>

    //补充代码

    function img(url){

     new Promise(resolve=>{

     resolve();

     var myImg=document.createElement('image');

     myImg.src=url;

     document.body.appendChild(myImg);

     setTimeout(function(){

     resolve();

     },1000)

     })

    }

    

    img('http://img1.sycdn.imooc.com/climg//5b16558d00011ed506000338.jpg')

     .then(function(){

     console.log('第一张');

     return img('http://img1.sycdn.imooc.com/climg//5b165603000146ca06000338.jpg');

     })

     .then(function(){

     console.log('第二张');

     return img('http://img1.sycdn.imooc.com/climg//5b1656140001c89906000338.jpg');

     })

     .then(function(){

     console.log('图片加载完成');

     })

</script>

</body>

</html>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,问题解答如下:

1.这是因为函数img没有返回Promise对象,那么返回值为undefined,undefined后面不能调用then方法,所以报错。

2.图片标签应该使用img。

3.resolve有一个多余的需要去掉

针对以上问题,如下修改:

http://img1.sycdn.imooc.com//climg/5eb660aa09e141b409020412.jpg

如果我的回答帮助到了你,欢迎采纳,祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师