Question
My print.swift is like:
// print.swift
let result = Task {
for index in 0..<3 {
try await Task.sleep(nanoseconds: 1 * 1000 * 1000 * 1000)
print("Compile \(index)")
}
return 0
}
_ = try await result.value
If I use: swift print.swift, then the output will be:
# wait 1s
Compile 1
# wait 1s
Compile 2
# wait 1s
Compile 3
But if I use swift print.swift | cat, then the output will be:
# wait 3s
Compile 1
Compile 2
Compile 3
Why? What's the difference?
Things I've try
- Changing
print.swiftto this doesn't change the result:
// print.swift
import Foundation
for index in 0..<3 {
usleep(1000000)
print("Compile \(index)")
}
- using
print.shorprint.sh | cathas the same output which met my expectation:
# print.sh
for index in {0..2}; do
sleep 1
echo "Compile $index"
done
outputs:
# wait 1s
Compile 1
# wait 1s
Compile 2
# wait 1s
Compile 3
- compile
print.swifttoprint.swift.ousingswiftcdoesn't change the result.