Discuss / JavaScript / 异步行为try catch 把握不住,交给系统处理。

异步行为try catch 把握不住,交给系统处理。

Topic source

Junes_99994

#1 Created at ... [Delete] [Delete and Lock User]

!async function() {

                const $btn = $('#calc');

                $btn.off('click');

                $btn.after("<p></p>")

                try { //await

                    await new Promise(r =>

                        $btn.click(function() {

                            let

                                x = parseFloat($('#x').val()),

                                y = parseFloat($('#y').val()),

                                r;

                            if (isNaN(x) || isNaN(y)) {

                                try {

                                    $(this).next().text('输入有误');

                                    throw new Error('输入有误');

                                } catch (e) {

                                    $(this).next().text('输你妹');

                                    throw new Error('输你妹');

                                }

                            } else {

                                r = x + y;

                                $(this).next().text('计算结果:' + r);

                            }

                        })

                        .click(function() {

                            r();

                        }));

                    console.log("hi");

                } catch (e) {

                    console.log(e)

                    $btn.next().text('输入有误!');

                }

            }();


  • 1

Reply